473,789 Members | 2,728 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Portable use of fread/fwrite with structs

Is there a portable way to use binary files?
By portability I mean:
Machine A (a supercomputer) with some compiler, my job runs there and
outputs several megabytes of data. To save space and for better
seekability, I use a binary format.
Machine B (my desktop) with some other compiler analizes this data.
So far I assumed that both architectures have the standard
representation of the float, double and int types.
I would like to use now structs, where the programs compiled for the
two machines may use different
representation, even if they both obey to the same (implementation
defined) arithmetic standard.
Can one still use structs in fread/fwrite, or only a member-by-member
input/output is possible.

Nov 15 '05 #1
2 2594
mazsx wrote:
Is there a portable way to use binary files?
By portability I mean:
Machine A (a supercomputer) with some compiler, my job runs there and
outputs several megabytes of data. To save space and for better
seekability, I use a binary format.
Machine B (my desktop) with some other compiler analizes this data.
So far I assumed that both architectures have the standard
representation of the float, double and int types.
I would like to use now structs, where the programs compiled for the
two machines may use different
representation, even if they both obey to the same (implementation
defined) arithmetic standard.
Can one still use structs in fread/fwrite, or only a member-by-member
input/output is possible.


I'd suggest storing things in network byte order simply because you can
then use htons/ntohs & friends without writing your own endian
conversion libraries.

As for storing structs, I'd suggest properly serialising them by
writing & reading member-by-member. This means you need a
struct-to-bytearray & bytearray-to-struct function for each struct you
intend to store.

Also, I strongly recommend that you document the file representation of
the struct so
1. you have a reference for debugging your input/output.
2. code written in other languages in the future can read the file

Nov 15 '05 #2
mazsx wrote:
Is there a portable way to use binary files?
By portability I mean:
Machine A (a supercomputer) with some compiler, my job runs there and
outputs several megabytes of data. To save space and for better
seekability, I use a binary format.
Machine B (my desktop) with some other compiler analizes this data.
So far I assumed that both architectures have the standard
representation of the float, double and int types.
I would like to use now structs, where the programs compiled for the
two machines may use different
representation, even if they both obey to the same (implementation
defined) arithmetic standard.
Can one still use structs in fread/fwrite, or only a member-by-member
input/output is possible.


This is a faq (see question 20.5)
http://www.eskimo.com/~scs/C-faq/top.html
for xdr implementation see for example http://www.ossp.org/ (library
called OSSP xds).

with respect,
Toni Uusitalo
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
15360
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same location in the file. What is happening, is I can read the file, either in entirety or only part of it. And no matter what I try setting the position to, using fsetpos, it always gets set back to the very beginning of the file. I
2
15578
by: Luc Holland | last post by:
Hey, I'm working on a program that reads a binary file. It's opened with ==== if ((f1=fopen(argv,"rb"))==NULL) { fprintf(stderr,"Error opening %s for reading . . .\n",argv); exit(2); } ==== The structure of the file is:
4
9290
by: janssenssimon | last post by:
//de structure om de highscores in op de slagen typedef struct score{ char *naam; int veld; int score; struct score *volg; }HIGH; void toonhighscores(void)
2
6322
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
8
17194
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)? 1. Books and documentation on C tell me fread and fwrite is "binary". What does binary mean in this? Anything to do with opening a file in binary mode? 2. I'm also sure I've read somewhere on the net that fread and
2
3079
by: elisa | last post by:
Dear all, I have problems in writeing and reading a block of data (long array) with fread and fwrite. If I write and read an integer array, everything looks fine, but when I try long array, sth strange happen, the data read from a file is not the same the data I supposed to write. Since I can't view the binary data file, I don't know what's wrong. Here is my code, please help me, Thanks a lot! Elisa #include <stdio.h> #define...
5
2372
by: loudking | last post by:
Dear all, I encountered a problem with fread and fwrite. If I am going to copy a file using the same string, it will succeed char *file_content; struct stat buf; FILE *fp, *new_fp; fread(file_content, sizeof(char), buf.st_size, fp);
30
14285
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
4
3777
by: Highlander2nd | last post by:
Hello there. I'm Andrew Lucas, I'm a programmer for Half-Life. I've been working on stencil shadows lately, and I've been having problems saving mesh data for my models. When I store mesh data, I have to store data for each submodel and their respective triangle data, wich are the vertex and neighbor(triangles sharing a common edge) indexes. Now, I'm storing this data in these struct's: struct Face { Face() {} Face(GLushort v0,...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10142
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.