473,320 Members | 1,846 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,320 software developers and data experts.

Help with binary I/O


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<intint_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<intint_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
Nov 11 '08 #1
4 1415
zr
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<intint_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<intint_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));
}
Nov 11 '08 #2
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<intint_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<intint_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.

Nov 11 '08 #3
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:ja*********@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
Nov 11 '08 #4
On Nov 11, 7:53*pm, zr <zvir...@gmail.comwrote:
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<intint_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<intint_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.

Nov 12 '08 #5

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

Similar topics

9
by: Darren | last post by:
Hi wondering if anyone can help. What i'm trying to do is get a company from a MSSQL database with the COMPANYNO which is a 'uniqueidentifier'. Then with this COMPANYNO I want to reference it to...
4
by: Paul | last post by:
Hi, (First apologies if this is not the most relevant place to post this but I wasn't sure of where was and I am writing my app in VB.) I'm attempting to parse a binary file for which I have...
1
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am > cs students and are now facing difficult problems in understanding > what a binary tree is, how it works, and the algorithm...
8
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am cs students and are now facing difficult problems in understanding what a binary tree is, how it works, and the algorithm to...
2
by: Lisa Pearlson | last post by:
Hi, My php application (on Apache/Linux) needs to do the following: The PHP script receives a request from a client (binary), asking for certain records of data. My PHP script loops through...
1
by: al mwaj3 | last post by:
please I need help in this problem, I must send it tomorrow the problem is (1) Write a Java class called: Binary that has two data fields: an integer array, and an integer size as well as the...
0
by: aled | last post by:
I'm new to C++ and need help with a binary program, represented in the program as an array of bits (0's and 1's). It allows the user to input a binary word, then do some calculations or...
1
by: satan | last post by:
I need to write the definition of the method leavesCount that takes as a parameter a reference of the root node of a binary tree and returns the numbers of leaves in the binary tree. This what i get...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
2
by: jmskater | last post by:
I have this so far: #include <iostream> using namespace std; void main() { int bit;
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...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.