[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Help with binary I/O

joseph.redimerio

11/11/2008 8:06:00 AM


Hi there,

I'm trying to save a vector data structure to a binary file, then read
it back and see if all the contents are intact. So I'm trying to
following code below :


#include "stdafx.h"
#include <stdio.h>
#include <vector>
#include <iostream>
#include <fstream>

using namespace std;

void main() {

//
// Create a test vector of integers and populate with test data
//
vector<int> int_ls;

int_ls.push_back(100);
int_ls.push_back(200);
int_ls.push_back(300);
int_ls.push_back(400);
int_ls.push_back(500);

//
// Dump the contents of the integer vector to a binary file
//
fstream binfile("fstream.out", ios::out|ios::binary);
binfile.seekp(0, ios::end);
binfile.write((char*)&int_ls,sizeof(int_ls));
binfile.close();


//
// Re-open the binary file and this time read the values and
store again in vector structure
//
vector<int> int_ls2;
fstream binfile2("fstream.out", ios::out|ios::in|ios::binary);
binfile2.seekg(0, ios::beg);

while (!binfile2.eof()) {
binfile2.read((char*)&int_ls2, sizeof(int_ls2));
}

binfile2.close();


//
// Now let's display what we just read in
//
int DATA_SIZE = int_ls2.size();

for (i=0; i<DATA_SIZE; i++) {
printf("\n int_ls2[%d] --> %d", i, int_ls2[i]);
};

}


** The above code manages to save to a file and even retrieve all the
values, but somehow I get an assert before it terminates.

** More info: I'm compiling it using Visual C++ 2008 as a Console
Application.

Thanks in Advance,
Joseph
5 Answers

Hendrik Schober

11/11/2008 8:52:00 AM

0

joseph.redimerio@baesystems.com wrote:
> [...]
> vector<int> int_ls;
>
> int_ls.push_back(100);
> int_ls.push_back(200);
> int_ls.push_back(300);
> int_ls.push_back(400);
> int_ls.push_back(500);
>
> //
> // Dump the contents of the integer vector to a binary file
> //
> fstream binfile("fstream.out", ios::out|ios::binary);
> binfile.seekp(0, ios::end);
> binfile.write((char*)&int_ls,sizeof(int_ls));

Here you write 'std::vector's internal data structures
(most likely a bunch of pointers) instead of the data they
refer to. (Put a breakpoint on that line and in the debugger
look at "int_ls,!" (without the quotes) in order to see what
'std::vector' is made of.
You get access to the address of the first element by using
'&int_ls[0]' or '&*int_ls.begin()'. Since 'std::vector's
data is supposed to be contiguous, this can be treated as
if it was a plain old (dynamic) array.

> [...]
> Thanks in Advance,

HTH

> Joseph

Schobi

zr

11/11/2008 8:53:00 AM

0

On Nov 11, 10:05 am, joseph.redime...@baesystems.com wrote:
> Hi there,
>
> I'm trying to save a vector data structure to a binary file, then read
> it back and see if all the contents are intact.  So I'm trying to
> following code below :
>
> #include "stdafx.h"
> #include <stdio.h>
> #include <vector>
> #include <iostream>
> #include <fstream>
>
> using namespace std;
>
> void main() {
>
>       //
>       // Create a test vector of integers and populate with test data
>       //
>       vector<int> int_ls;
>
>       int_ls.push_back(100);
>       int_ls.push_back(200);
>       int_ls.push_back(300);
>       int_ls.push_back(400);
>       int_ls.push_back(500);
>
>       //
>       // Dump the contents of the integer vector to a binary file
>       //
>       fstream binfile("fstream.out", ios::out|ios::binary);
>       binfile.seekp(0, ios::end);
>       binfile.write((char*)&int_ls,sizeof(int_ls));
>       binfile.close();
>
>       //
>       // Re-open the binary file and this time read the values and
> store again in vector structure
>       //
>       vector<int> int_ls2;
>       fstream binfile2("fstream.out", ios::out|ios::in|ios::binary);
>       binfile2.seekg(0, ios::beg);
>
>       while (!binfile2.eof()) {
>            binfile2.read((char*)&int_ls2, sizeof(int_ls2));
>       }
>
>       binfile2.close();
>
>       //
>       // Now let's display what we just read in
>       //
>       int DATA_SIZE = int_ls2.size();
>
>       for (i=0; i<DATA_SIZE; i++) {
>             printf("\n int_ls2[%d] --> %d", i, int_ls2[i]);
>       };
>
> }
>
> ** The above code manages to save to a file and even retrieve all the
> values, but somehow I get an assert before it terminates.
>
> ** More info: I'm compiling it using Visual C++ 2008 as a Console
> Application.
>
> Thanks in Advance,
> Joseph

A couple of errors i spotted:

1) binfile.write((char*)&int_ls,sizeof(int_ls));
sizeof(int_ls) returns the size of the container object. What you
probably meant was the size of the objects stored by the container
objects. A quick solution (perhaps not the most elegant) is:
binfile.write((char*)&int_ls.front(), int_ls.size()*sizeof int_ls[0]);

2) The while loop: similar problem as above. Try:
int buffer;
binfile2.read((char*)&buffer, sizeof(buffer));
while (!binfile2.eof()) {
int_ls2.push_back(buffer);
binfile2.read((char*)&buffer, sizeof(buffer));
}

Rolf Magnus

11/11/2008 11:06:00 AM

0

zr wrote:

> On Nov 11, 10:05 am, joseph.redime...@baesystems.com wrote:
>> Hi there,
>>
>> I'm trying to save a vector data structure to a binary file, then read
>> it back and see if all the contents are intact. So I'm trying to
>> following code below :
>>
>> #include "stdafx.h"
>> #include <stdio.h>
>> #include <vector>
>> #include <iostream>
>> #include <fstream>
>>
>> using namespace std;
>>
>> void main() {

main() must return int in C++.

>> //
>> // Create a test vector of integers and populate with test data
>> //
>> vector<int> int_ls;
>>
>> int_ls.push_back(100);
>> int_ls.push_back(200);
>> int_ls.push_back(300);
>> int_ls.push_back(400);
>> int_ls.push_back(500);
>>
>> //
>> // Dump the contents of the integer vector to a binary file
>> //
>> fstream binfile("fstream.out", ios::out|ios::binary);
>> binfile.seekp(0, ios::end);
>> binfile.write((char*)&int_ls,sizeof(int_ls));
>> binfile.close();
>>
>> //
>> // Re-open the binary file and this time read the values and
>> store again in vector structure
>> //
>> vector<int> int_ls2;
>> fstream binfile2("fstream.out", ios::out|ios::in|ios::binary);
>> binfile2.seekg(0, ios::beg);
>>
>> while (!binfile2.eof()) {
>> binfile2.read((char*)&int_ls2, sizeof(int_ls2));
>> }
>>
>> binfile2.close();
>>
>> //
>> // Now let's display what we just read in
>> //
>> int DATA_SIZE = int_ls2.size();
>>
>> for (i=0; i<DATA_SIZE; i++) {
>> printf("\n int_ls2[%d] --> %d", i, int_ls2[i]);
>> };
>>
>> }
>>
>> ** The above code manages to save to a file and even retrieve all the
>> values, but somehow I get an assert before it terminates.
>>
>> ** More info: I'm compiling it using Visual C++ 2008 as a Console
>> Application.
>>
>> Thanks in Advance,
>> Joseph
>
> A couple of errors i spotted:
>
> 1) binfile.write((char*)&int_ls,sizeof(int_ls));
> sizeof(int_ls) returns the size of the container object. What you
> probably meant was the size of the objects stored by the container
> objects. A quick solution (perhaps not the most elegant) is:
> binfile.write((char*)&int_ls.front(), int_ls.size()*sizeof int_ls[0]);

You shouldn't use int_ls.front() for one function arguemnt and int_ls[0] for
the other. They are both the same thing.

> 2) The while loop: similar problem as above. Try:
> int buffer;
> binfile2.read((char*)&buffer, sizeof(buffer));
> while (!binfile2.eof()) {
> int_ls2.push_back(buffer);
> binfile2.read((char*)&buffer, sizeof(buffer));
> }

This is more complicated than necessary. Also, it will result in an endless
loop filling up all available memory if any error happens to the stream
during reading. I'd rather suggest:

while (binfile2.read((char*)&buffer, sizeof(buffer)))
{
int_ls2.push_back(buffer);
}

Then after the loop, check if eof was reached or some error happened.

James Kanze

11/11/2008 9:51:00 PM

0

On Nov 11, 9:05 am, joseph.redime...@baesystems.com wrote:

> I'm trying to save a vector data structure to a binary file,
> then read it back and see if all the contents are intact.

What is the format of the binary file? (Until you've answered
that, there's no way we can tell you what you have to do.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

joseph.redimerio

11/12/2008 1:45:00 AM

0

On Nov 11, 7:53 pm, zr <zvir...@gmail.com> wrote:
> On Nov 11, 10:05 am, joseph.redime...@baesystems.com wrote:
>
>
>
>
>
> > Hi there,
>
> > I'm trying to save a vector data structure to a binary file, then read
> > it back and see if all the contents are intact.  So I'm trying to
> > following code below :
>
> > #include "stdafx.h"
> > #include <stdio.h>
> > #include <vector>
> > #include <iostream>
> > #include <fstream>
>
> > using namespace std;
>
> > void main() {
>
> >       //
> >       // Create a test vector of integers and populate with test data
> >       //
> >       vector<int> int_ls;
>
> >       int_ls.push_back(100);
> >       int_ls.push_back(200);
> >       int_ls.push_back(300);
> >       int_ls.push_back(400);
> >       int_ls.push_back(500);
>
> >       //
> >       // Dump the contents of the integer vector to a binary file
> >       //
> >       fstream binfile("fstream.out", ios::out|ios::binary);
> >       binfile.seekp(0, ios::end);
> >       binfile.write((char*)&int_ls,sizeof(int_ls));
> >       binfile.close();
>
> >       //
> >       // Re-open the binary file and this time read the values and
> > store again in vector structure
> >       //
> >       vector<int> int_ls2;
> >       fstream binfile2("fstream.out", ios::out|ios::in|ios::binary);
> >       binfile2.seekg(0, ios::beg);
>
> >       while (!binfile2.eof()) {
> >            binfile2.read((char*)&int_ls2, sizeof(int_ls2));
> >       }
>
> >       binfile2.close();
>
> >       //
> >       // Now let's display what we just read in
> >       //
> >       int DATA_SIZE = int_ls2.size();
>
> >       for (i=0; i<DATA_SIZE; i++) {
> >             printf("\n int_ls2[%d] --> %d", i, int_ls2[i]);
> >       };
>
> > }
>
> > ** The above code manages to save to a file and even retrieve all the
> > values, but somehow I get an assert before it terminates.
>
> > ** More info: I'm compiling it using Visual C++ 2008 as a Console
> > Application.
>
> > Thanks in Advance,
> > Joseph
>
> A couple of errors i spotted:
>
> 1) binfile.write((char*)&int_ls,sizeof(int_ls));
> sizeof(int_ls) returns the size of the container object. What you
> probably meant was the size of the objects stored by the container
> objects. A quick solution (perhaps not the most elegant) is:
> binfile.write((char*)&int_ls.front(), int_ls.size()*sizeof int_ls[0]);
>
> 2) The while loop: similar problem as above. Try:
>           int buffer;
>           binfile2.read((char*)&buffer, sizeof(buffer));
>           while (!binfile2.eof()) {
>                   int_ls2.push_back(buffer);
>                   binfile2.read((char*)&buffer, sizeof(buffer));
>         }- Hide quoted text -
>
> - Show quoted text -



Thanks for the suggestions everyone !! I tried all of them, and I
found this one works the best! Thank you very much ZR.