Advertisement

02.12.2005 at 05:39PM PST, ID: 21312536
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

fread, fwrite on binary files

Tags: fread, fwrite, binary, c
Hello 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(struct myStruct) * 20);

 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);


 

Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: Kevin_P23
Solution Provided By: sunnycoder
Participating Experts: 2
Solution Grade: A
Views: 256
Translate:
Loading Advertisement...
02.12.2005 at 05:56PM PST, ID: 13296103

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2005 at 06:23PM PST, ID: 13296160

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2005 at 06:33PM PST, ID: 13296184

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2005 at 06:34PM PST, ID: 13296188

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2005 at 07:09PM PST, ID: 13296288

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2005 at 07:11PM PST, ID: 13296297

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2005 at 07:26PM PST, ID: 13296333

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2005 at 07:35PM PST, ID: 13296360

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2005 at 07:36PM PST, ID: 13296366

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2005 at 07:50PM PST, ID: 13296400

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2005 at 08:47PM PST, ID: 13296523

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.12.2005 at 09:02PM PST, ID: 13296550

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.12.2005 at 05:56PM PST, ID: 13296103

Rank: Sage

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?
 
02.12.2005 at 06:23PM PST, ID: 13296160
The file is an unformatted binary file. Wouldn't this be the ideal situation to use fread?
 
02.12.2005 at 06:33PM PST, ID: 13296184

Rank: Sage

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
 
02.12.2005 at 06:34PM PST, ID: 13296188
>  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]
 
02.12.2005 at 07:09PM PST, ID: 13296288

> What I am interested in knowing is the format of the information organization file e.g.

the data file looks like this:

programming one^@^@^@^@^@^@^@^@^Cprogramming two^@^@^@^@^@^@^@^@^Cstudent name^@^@^@^@^@^@^@^@^@^@^@^A ...

This is more or less how the info looks the data file I'm playing with. Hope this clears things up.
 
02.12.2005 at 07:11PM PST, ID: 13296297

Rank: Sage

I do not see any ints in there which correspond to num

the ^@ are spaces or some special characters? Field width is fixed in file? No record delimiters?
 
02.12.2005 at 07:26PM PST, ID: 13296333
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^@^@^@^@^@^@^@^@^@^@^@^A
I know that the integer is suppose to be 1.





 
02.12.2005 at 07:35PM PST, ID: 13296360
in other words a binary file with a record being:

 char [20]   followed by a 4-byte integer with big end first
Assisted Solution
 
02.12.2005 at 07:36PM PST, ID: 13296366

Rank: Sage

Ok ... in that case, it will be best to use two freads corresponding to the two fwrites

fread does fit better as compared to fgets ...

using two freads will let you ignore structure padding issues as mysidia pointed out
Accepted Solution
 
02.12.2005 at 07:50PM PST, ID: 13296400
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?
 
02.12.2005 at 08:47PM PST, ID: 13296523

Rank: Sage

>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
 
02.12.2005 at 09:02PM PST, ID: 13296550
Thanks guys.

--Kevin
 
 
20080236-EE-VQP-29