Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Write struct to file

Posted on 2006-07-02
7
Medium Priority
?
637 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

609 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