Solved

Write struct to file

Posted on 2006-07-02
7
595 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

896 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

15 Experts available now in Live!

Get 1:1 Help Now