The file is an unformatted binary file. Wouldn't this be the ideal situation to use fread?
Main Topics
Browse All TopicsHello experts,
I'm new to C programming and have minimal experience with the fread and fwrite functions. I'm hoping someone out there can help me to see clearly how they work.
Suppose I have a data file where, at most, 20 groups of a string and then an integer are stored with a length of 20 and 4 bytes, respectively.
I figure to process the date, I would need a struct.
struct myStruct {
char string[20]; /* 20 bytes for the string */
int num; /* 4 bytes for in */
};
Now would the following correctly read the data?
==================
struct myStruct *temp;
temp = (struct myStruct*)malloc(sizeof(st
while (fread(temp, sizeof(struct myStruct), 20, data) != 0)
/* To write what is stored in element 1 of temp, i would do: */
fwrite((const void *) temp[1].string, 20, 1, file);
fwrite((const void *) &temp[1].num, 4, 1, file);
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
From your freads it does not quite look like unformatted binary file ... Pedantically it is a binary file but it has all ASCII text chars!!! ...
What I am interested in knowing is the format of the information organization file e.g.
bob \t 25
tom \t 32
or
bob25
tom32
or
bob 25 tom 32
.....
>Wouldn't this be the ideal situation to use fread?
Ideal situation will be when you wish to read in a jpg and you know the size .. I hope you get the idea
> while (fread(temp, sizeof(struct myStruct), 20, data) != 0)
> fwrite((const void *) temp[1].string, 20, 1, file);
> fwrite((const void *) &temp[1].num, 4, 1, file);
Actually, you're making an assumption about how the structure is aligned..
those two fwrites are basically only write if there's no padding between
the string and num members.
Where 'string' and 'num' are in the structure when you read
num will probably be aligned to the nearest word boundary
in reality, but you can't be sure, thus you should really do
fwrite(&temp[1], sizeof(struct myStruct), 1, file);
The result will still not be a platform-independent binary file, however
due to differences in the representation of the integer types
between the host byte order and the more standard (non-platform-specific)
network byte ordering
(functions htonl and ntohl [host to network long, network to host long]
and htons and ntohs [host to network short, etc]
> What I am interested in knowing is the format of the information organization file e.g.
the data file looks like this:
programming one^@^@^@^@^@^@^@^@^Cprogr
This is more or less how the info looks the data file I'm playing with. Hope this clears things up.
programming one^@^@^@^@^@^@^@^@^C
Here the string, obviously, is "programming one", and I know the integer in here is 3.
I guess ^@ fill up extra spaces to bring the string to 20, followed by 3 ^@ and then ^C (for 3).
Similarly, for
student name^@^@^@^@^@^@^@^@^@^@^@
I know that the integer is suppose to be 1.
Yeah, I'm sorry if I'm not being very clear. Some of this stuff is new to me and I don't really know how to explain-
So, if there are 10 such records in the data file, if I used that fread function from in my original post, everything will read into the struct ok? Also, could I then use printf on temp[1].string, for instance?
>if I used that fread function from in my original post, everything will read into the struct ok?
It should read ok, if there is no structure padding ... Use two different freads, one for string and one for num to be sure that you read in the values right irrespective of padding
>Also, could I then use printf on temp[1].string, for instance?
Yes
Business Accounts
Answer for Membership
by: sunnycoderPosted on 2005-02-12 at 17:56:20ID: 13296103
No, it will not read the data correctly in all instances ... only when all your strings are exactly 20 bytes and all your numbers are exactly 4 bytes in the file.
fread and fwrite are preferably used when binary data is being read/written ... We use them when
- data is binary
- we are not willing to associate a type with it yet
- we know the exact size ...
In your case, it will be far better if you use fgets to get a record in a buffer and then parse it and convert into appropriate data types (strtol, atoi etc)
Or use fscanf ... fscanf works exactly like scanf and similarly can have messy format strings if your reading requirements are a bit involved
What is the format of you file?