Solved

Write struct to file

Posted on 2006-07-02
7
603 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Estimate porting issues. 9 119
Adjust Mfcapp 29 166
C++ vs C compilers 13 159
Why isn't object file created? 6 57
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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

816 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

9 Experts available now in Live!

Get 1:1 Help Now