Solved

How to write a struct to a file?

Posted on 2006-07-13
12
285 Views
Last Modified: 2010-04-01
Hi!

I would like to know if there is any way to write a entira struct to a file.
I have a struct that i have got from a TCP connection it looks like this.

TCPConn2->ReadBuffer(VMes,sizeof(*mes));
mes = static_cast<Message*>(VMes);

The mes is the structure and it looks like this

typedef struct{
        __int32 Param_code;
        unsigned __int32 Time;
        __int32 Measure_Value;
}S_record;

typedef struct{
        //--------HEADERS-------
        unsigned __int16 STX;
      unsigned __int16 CRC;
        unsigned __int16 MachID;
        unsigned __int16 ClinicalSWID;
        unsigned __int32 MsgCounter;
        unsigned __int32 CmdCode;
        unsigned __int32 MsgInfo;
        unsigned __int32 Flags;
        unsigned __int32 PatId;
        //----------------------
        S_record records[412];  
}Message;

I would now like to write mes to a file. I know that i can use ifstream but then i have to convert each element to string if i have't missunderstod it.
Is there a way to write the data without converting it?
0
Comment
Question by:mikrodidakt
[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
  • 5
  • 4
  • 3
12 Comments
 
LVL 5

Accepted Solution

by:
bastibartel earned 250 total points
ID: 17098220
Hi mikrodidakt,

You can write the entire struct to file in a binary format. That is, if you are sure the struct will not change in the near future ;-)


#include <fstream>
Message Msg;

std::ofstream oStream;
oStream.open("c:\\full path\\out.txt",std::fstream::out|std::ios::binary);
oStream.write(Msg, sizeof(Msg));
oStream.close()

Please do verify that sizeof(Msg) returns the size of your struct, as I am not absolutely sure the embdeded space for S_record[412] will be reflected properly by sizeof()
Other than that, since you have no dynamic objects in your struct - you are all set.

Cheers!
Sebastian
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17098223
Uups, it's

oStream.open("c:\\full path\\out.txt",std::fstream::out|std::ios<wbr/>s::binary)<wbr/>;
                                                                                               ^
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17098230
aaahh forget the last post - (screw QuickPost !)
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 7

Expert Comment

by:nafis_devlpr
ID: 17098253
use ofstream and use the binary mode to write into the file, but in case you change the allignment or add anything else in the structure you will have to change the old files.

Nafis
0
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17098254
sorry bastibartel, i was a bit late than you.
0
 

Author Comment

by:mikrodidakt
ID: 17098296
thanks
0
 

Author Comment

by:mikrodidakt
ID: 17098334
But why would there be a problem if the size of the struct is changed then the sizeof(mes) will also be change?
0
 

Author Comment

by:mikrodidakt
ID: 17098375
sorry you have alreadt answered the question forget the last one thanks.
0
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17098379
when you change the struct, then the size is changed. You wont see any difference if you replace a variable with the same size. But the interpretation of the struct might change because of that
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17098585
Hi again,

> But why would there be a problem if the size of the struct is changed then the sizeof(mes) will also be change?
Yes that is correct - you just won't be able to import your 'old' structs anymore.

Owing to this potential problem, Microsoft prefixes many structs with a variable holding the struct's size.
Thus you can later determine (by importing the first say 4 bytes), whether a stored struct is compatible with the current version of the struct.

Cheers,
Sebastian
 

0
 

Author Comment

by:mikrodidakt
ID: 17098629
This is for programmers that might have the same problem as me in the future.
Her is my conde.

std::ofstream oStream;
AnsiString path = "C:\\Program\\Borland\\CBuilder6\\Projects\\Prismaflex\\Samples\\Sample";
path += IntToStr(messageNbr);

oStream.open(path.c_str(),std::fstream::out|std::ios::binary);
char* c_msg = (char*) msg;
oStream.write(c_msg, sizeof(msg));
oStream.close();
0
 

Author Comment

by:mikrodidakt
ID: 17099217
Forget the comment above it dont work!!!!!!!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

735 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