Solved

Write struct to file

Posted on 2006-07-02
7
584 Views
Last Modified: 2010-04-15
How do I write the following struct to a binary file in the correct byte order?  (Linux platform)
For example, the file should list the struct in the order shown below.

struct test_id      {
      unsigned int f1:3;
      unsigned int f2:4;
      unsigned int f3:5;
      unsigned int f4:8;
      unsigned int f5:4;
      unsigned int f6:12;
} test;

      FILE *fp = fopen("testfile", "w");
      test.f1= 2;
      test.f2 = 0;
      test.f3 = 22;
      test.f4= 23;
      test.f5 = 4;
      test.f6= 200;
0
Comment
Question by:jewee
  • 3
  • 3
7 Comments
 

Expert Comment

by:dark_archon
ID: 17026121
Have you tried fwrite()? The following MAY work. (I haven't tested it.)

fwrite(&test, sizeof( test ), 1, fp);
0
 

Expert Comment

by:dark_archon
ID: 17026123
The order that the elements are stored in memory is probably OS and/or compiler dependent, too.
0
 

Assisted Solution

by:dark_archon
dark_archon earned 100 total points
ID: 17026126
I've now tested it, and it works. Just use fread() to read it all into another struct:

fread( &test, sizeof( test ), 1, fp );
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 8

Expert Comment

by:manish_regmi
ID: 17026151
>> 've now tested it, and it works. Just use fread() to read it all into another struct:
I wont always work.
Have you tried the program to store in Big Endian machine and program to retrieve in little endean machine.

jewee:
You need to take a design decision to store the multibyte values in either bigendean or Little endean.

then,
1) then before writing convert your multibyte data to your endeaness of choice.
You should make a macro to convert from host byte order to you chosen byte order.

2) write it to a file.

While retrieveing,
1) read from the disk.
2) convert from on disk byte order to host byte order.

Can u use some padding to your structure elements and make 8 16 or 32 bit values.

regards
Manish Regmi
0
 

Author Comment

by:jewee
ID: 17026165
Thank you for the recommendations.  I will have to store it on a little endian machine.

I have little experience with this.  Could you provide me with sample code for the conversion?

Also, in regards to padding - I could possibly insert 0 filled padding at the end.

0
 
LVL 8

Accepted Solution

by:
manish_regmi earned 400 total points
ID: 17026200
like,

#if BYTE_ORDER == LITTLE_ENDEAN

#define htole_w(val) (val)
#define htole_d(val) (val)
...
...

#else
#define htole_w(val) ((val << 8) & 0xFF00 | ((val >> 8) &0xFF)) for 16 bit
......

#endif


regards
Manish
0
 
LVL 8

Expert Comment

by:manish_regmi
ID: 17026207
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now