Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

simulating the leading and trailing length fields in a variable-length unformatted file with C

gregfi04
Beginner
729 Views
Hi folks,

I'm trying to port a code from HP-UX over to Linux/ifort. Each binary file that this code writes is prefixed by a ~100 character string that contains some critical information (the date of execution, the version of the code that created it, and a traceable certification that the file was created by a controlled, V&V'd executable). Unfortunately, my predecesor saw fit to write the part that deals with this ~100 character string in C. These strings need to be read by other Fortran codes.

Where I'm getting hung up (I think) is on these 4-byte leading/trailing length fields (discussed in the "Record Types" manual page). The C code that works on the HP-UX machines looks something like this (cfgstrg is the desired string to be written - these strings are the same length every time):

strConv[0]=(char)('�');
strConv[1]=(char)('�');
strConv[2]=(char)('�');
strConv[3]=(char)('u');

fwrite(strConv, sizeof(char)*4, 1, wFile);
fwrite(cfgstrg, sizeof(char)*cfglen, 1, wFile);
fwrite(strConv, sizeof(char)*4, 1, wFile);


Our ifort/Linux system doesn't seem to dig that. I've poked around online a fair amount, but I'm having trouble finding exactly what's supposed to go in those leading/trailing length fields. Could someone point me in the right direction? I've pulled a fair amount of my own hair out diagnosing and trying to solve this one. Any help is much appreciated.

Thanks,
Greg
0 Kudos
4 Replies
Steven_L_Intel1
Employee
729 Views
Looks like big-endian 32-bit record lengths. If you open the file with CONVERT='BIG_ENDIAN' and FORM='UNFORMATTED', it should read fine.
0 Kudos
gregfi04
Beginner
729 Views
Steve,

Excellent! Yes, it works.

But ideally, I'd rather have the code generate leading/trailing fields in the new native format, rather than carry over the old, BIG_ENDIAN formatting. The new system is going to be x86_64 linux. Using the existing C code, what would be the appropriate 4-byte fields for that? Also, this particular code only seems to work when compiled in 32-bit mode. Would that affect the leading/trailing fields?

Thanks, you're a lifesaver.

Greg
0 Kudos
Steven_L_Intel1
Employee
729 Views
There's no difference in the record structure on x64. Both 32- and 64-bit compilers support record lengths larger than 2GB using a "segment" strategy that is described in the documentation. The layout is compatible with 32-bit lengths for normal size records.

If you want to write these headers in C, just write the length as an "int".
0 Kudos
gregfi04
Beginner
729 Views
Writing the length as an "int" did the trick. Thanks, again!
0 Kudos
Reply