473,287 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

classes wrting/reading to binary file

Hi all,

[code snippet appended at the end.)
my question:
A class has a few string variables with not know length at design
time.

Now I declare lets say a 1000 of those classes and put them in a vector.

Can I write that vector directly to a binary file?
(.wright((char *)&vector,sizeof (vector)))

Currently I write the classes to the file (iterate over the vector).
As a safety I write the length of the variable strings before the class
so that when I read the file I can create a string thats large enough,
using: .resize(length_of_string,'\0')

The code that I wrote works. If I leave a function out namely
files.clear();Could someone explain why it breaks?
Improvement on the code itself is always welcome.

My system:Linux 2.6 gnu compiler.
This is the code snipped that I wrote:
FileDes is the class with three strings (declared as "string")
the normal members functions are present get*data*, set*data*, operator<*>,...

ItsFile is declared as fstream in the class datafile (again a class with
normal member functions.)

void
datafile::ReadFromFile (vector < FileDes > &vecfd)
{
FileStatus (input, open); // opens a file in binary mode for input
int length, a, b, c;
//read the size of the vector.
//why? so only one (re)allocation is needed
ItsFile.read ((char *) &length, sizeof length);
vecfd.reserve (length);
for (int i = 0; i < length; i++) //reading from a file
{

//read the length of the 3 strings first:
ItsFile.read ((char *) &a, sizeof a);
ItsFile.read ((char *) &b, sizeof b);
ItsFile.read ((char *) &c, sizeof c);
FileDes temp(a,b,c) ; //expand strings to hold enough data
FileDes temp; //works also. does string expand automatically
//in this situation(or code)

//read the class and add to the vector
ItsFile.read ((char *) &temp, sizeof temp);

//if the program breaks it happens on the next line:
vecfd.push_back (temp);

}

}

int
main ()
{
datafile Filed;
int length;
vector < FileDes > files;
vector< FileDes > filesa;
Filed.GetFileList (files);
Filed.WriteToFile (files);
//The above all works fine checked it with displaying the strings when
//I knew what it had to contain

files.clear();//if left out the program works.
//if present it breaks at run time,
// executing the next line of code.
//break point in void datafile::ReadFromFile... at vecfd.push_back (temp);

Filed.ReadFromFile (filesa);

/*

DO SOME STUFF
*/
return 0;
}
Jul 22 '05 #1
4 3125
On Thu, 01 Jan 2004 07:52:26 +0000, nightflyer wrote:
Hi all,

[code snippet appended at the end.)
my question:
A class has a few string variables with not know length at design
time.

Now I declare lets say a 1000 of those classes and put them in a vector.

Can I write that vector directly to a binary file?
(.wright((char *)&vector,sizeof (vector)))


No, and this seems also to be why your program breaks. It seems you try
to modify string objects directly or something like that. You cannot just
stream out a class and hope it works. It doesn't. It only works for PODs
(search google on that) and then only if the POD does not contain
pointers. Both the string and the vector class break both rules.

Besides, even for the cases where it does work, it only works when the
receiver has the same binary layout as the sender. This often is not very
important, but it becomes /very/ important when this problem occurs on a
network connection. Same problem, different context only.

What you have to do is what you already did for the vector class. Stream
out using your own data format. A text-only format often works very well
(diskspace is cheap) and is very practical. But binary is possible as well
and much easier to program.

[ As an aside, you may want to take a look at XML libraries, they simplify
these kind of problems considerably, if at the price of a steep learning
curve. ]

Streaming out a string could be done something like this (errorchecking
omitted):

void so(std::ostream o, const std::string &s) {
size_t len = s.size();
o.write(reinterpret_cast<const char *>(&len), sizeof(len));
o.write(s.c_str(), len);
}

Reading back in is slightly trickier, we are not allowed to modify what
c_str() points to, nor can we assume safely we can use &s[0] as a pointer
to a continous array, so we use an intermediary to hold the contents of
the string:

void si(std::istream i, std::string &s) {
size_t len;
i.read(reinterpret_cast<const char *>(&len), sizeof(len));
vector<char> v(len);
i.read(&v[0], len);
s = string(v.begin(), v.end());
}

This code is untested, may contain bugs, may contain big errors. But you
probably get the basic idea.

It also might be a good idea to insert some markers in the stream for
better error checking. A magic number (signature) and version at the start
of the file are probable the least, but also sufficient for your problem.
When reading the file back, you check to see if the magic number is there
(high probablility the file was actually written by your program) and
check the version number (what version of the data-layout in the file are
we using, don't use program version number here).

HTH,
M4

Jul 22 '05 #2
On Thu, 01 Jan 2004 15:24:52 +0100, Martijn Lievaart wrote:
void so(std::ostream o, const std::string &s) {
void so(std::ostream & o, const std::string &s) {
void si(std::istream i, std::string &s) {


void si(std::istream & i, std::string &s) {

Oops.

M4

Jul 22 '05 #3
On Thu, 01 Jan 2004 16:06:29 +0100, Martijn Lievaart wrote:
On Thu, 01 Jan 2004 15:24:52 +0100, Martijn Lievaart wrote:
void so(std::ostream o, const std::string &s) {


void so(std::ostream & o, const std::string &s) {
void si(std::istream i, std::string &s) {


void si(std::istream & i, std::string &s) {

Oops.

M4


In void si(...):
i.read(reinterpret_cast<const char *>(&len), sizeof(len));

change to:
i.read(reinterpret_cast< char *>(&len), sizeof(len));

know it works.
thanks a lot for the help.
Jul 22 '05 #4
On Thu, 01 Jan 2004 19:46:25 +0000, nightflyer wrote:
In void si(...):
i.read(reinterpret_cast<const char *>(&len), sizeof(len));

change to:
i.read(reinterpret_cast< char *>(&len), sizeof(len));


Ahhh, damn! Next time I'll compile the stuff first.... :-)

Glad to be of service.

M4

Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
2
by: Jacek Dziedzic | last post by:
Hello! Suppose I have a class that contains only public members of builtin types and a default constructor. Because of the default constructor it is no longer an aggregate and therefore no...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
4
by: joey.powell | last post by:
I am writing a class that will do some binary file IO. The class will need to read a header from the binary file, and it will also need to read a varying number of records in the file. I currently...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.