Binary data in std::list

E

emanshu

Hi,

i am storing data read from some file (text,pdf,gif...) into std::list
using push_back function available for list.
and my list type is string.( std::list<string> mylist)
after storing whole file in list i am trying to writing the data stored
in list into new file of same format.
if the source file is text then destination new file obtained is
correct but if it is pdf then the destination file obtained is not
correct, even size is less than the original file.

i am opening the file using fopen() call in BInary Mode always..

well, if i write the data without using the std::list, i am getting the
expected correct results.
i am worrying why this is not working in case of std::list if data is
non textual..

if anybody is having any idea i will really appreciate the response.

thanks,
Munish
 
M

mlimber

emanshu said:
Hi,

i am storing data read from some file (text,pdf,gif...) into std::list
using push_back function available for list.
and my list type is string.( std::list<string> mylist)
after storing whole file in list i am trying to writing the data stored
in list into new file of same format.
if the source file is text then destination new file obtained is
correct but if it is pdf then the destination file obtained is not
correct, even size is less than the original file.

i am opening the file using fopen() call in BInary Mode always..

well, if i write the data without using the std::list, i am getting the
expected correct results.
i am worrying why this is not working in case of std::list if data is
non textual..

if anybody is having any idea i will really appreciate the response.

thanks,
Munish

Can you post the code for your read and write routines? Also, the
preferred file handling methods for C++ are fstreams not FILEs.

Cheers! --M
 
M

Mike Wahler

emanshu said:
Hi,

i am storing data read from some file (text,pdf,gif...) into std::list
using push_back function available for list.
and my list type is string.( std::list<string> mylist)
after storing whole file in list i am trying to writing the data stored
in list into new file of same format.
if the source file is text then destination new file obtained is
correct but if it is pdf then the destination file obtained is not
correct, even size is less than the original file.

i am opening the file using fopen() call in BInary Mode always..

well, if i write the data without using the std::list, i am getting the
expected correct results.
i am worrying why this is not working in case of std::list if data is
non textual..

if anybody is having any idea i will really appreciate the response.

You have a bug on line 42. Fix it.

-Mike
 
E

emanshu

Hi,

here is the code snipplets..
int i=0;

while( i < Source_File_Size){
bytesread=readFile(buff, 1400);
i=i+bytesread;
mylist.push_back(buff);
}

openFile(NewFileName in Binary mode){
std::string buff;
std::list<std::string>::const_iterator rcv it;
for (it.begin() to it.end()){
buff=*it;
writeFile(buff,buff.length());
}
this code snipplets generate the new file correctly if data is
textual...but does not generate correct data if it is binary..

case2:
also, if i do fow:
while( i < Source_File_Size){
bytesread=readFile(buff, 1400);
i=i+bytesread;
writeNewfile(buff,bytesread);
}
in case 2 i am not using std::list rather i am directly writing to file
the data read. and this code creates new file correctly for all types
of data(text,pdf,img..)

please help...

thanks,
munish
 
M

mlimber

emanshu said:
Hi,

here is the code snipplets..
int i=0;

while( i < Source_File_Size){
bytesread=readFile(buff, 1400);
i=i+bytesread;
mylist.push_back(buff);
}

openFile(NewFileName in Binary mode){
std::string buff;
std::list<std::string>::const_iterator rcv it;
for (it.begin() to it.end()){
buff=*it;
writeFile(buff,buff.length());
}
this code snipplets generate the new file correctly if data is
textual...but does not generate correct data if it is binary..

case2:
also, if i do fow:
while( i < Source_File_Size){
bytesread=readFile(buff, 1400);
i=i+bytesread;
writeNewfile(buff,bytesread);
}
in case 2 i am not using std::list rather i am directly writing to file
the data read. and this code creates new file correctly for all types
of data(text,pdf,img..)

please help...

thanks,
munish

What does readFile do? What does writeNewFile do? What type and size is
buff? etc. etc. If you post the actual code, we can provide a lot more
help.

Cheers! --M
 
M

mlimber

emanshu said:
Hi,

i am storing data read from some file (text,pdf,gif...) into std::list
using push_back function available for list.
and my list type is string.( std::list<string> mylist)
after storing whole file in list i am trying to writing the data stored
in list into new file of same format.
if the source file is text then destination new file obtained is
correct but if it is pdf then the destination file obtained is not
correct, even size is less than the original file.

i am opening the file using fopen() call in BInary Mode always..

well, if i write the data without using the std::list, i am getting the
expected correct results.
i am worrying why this is not working in case of std::list if data is
non textual..

if anybody is having any idea i will really appreciate the response.

thanks,
Munish

BTW, you probably don't want to use std::string for this purpose. Try
std::vector<char> instead.

Cheers! --M
 
D

Daniel Kay

emanshu said:
Hi,

here is the code snipplets..
int i=0;

while( i < Source_File_Size){
bytesread=readFile(buff, 1400);
i=i+bytesread;
mylist.push_back(buff);
}

openFile(NewFileName in Binary mode){
std::string buff;
std::list<std::string>::const_iterator rcv it;
for (it.begin() to it.end()){
buff=*it;
writeFile(buff,buff.length());
}
this code snipplets generate the new file correctly if data is
textual...but does not generate correct data if it is binary..

case2:
also, if i do fow:
while( i < Source_File_Size){
bytesread=readFile(buff, 1400);
i=i+bytesread;
writeNewfile(buff,bytesread);
}
in case 2 i am not using std::list rather i am directly writing to file
the data read. and this code creates new file correctly for all types
of data(text,pdf,img..)

Hello emanshu!

I didn't manage to understand 100% of what you are doing here. If in the
first while loop you are using a normal char array this might be the
problem. When pushing that char array into the list, it is handles as
zero terminated c-string. And every now and then you are missing data.
Just an idea...

CU,
Daniel Kay
 
J

John Harrison

Daniel said:
Hello emanshu!

I didn't manage to understand 100% of what you are doing here. If in the
first while loop you are using a normal char array this might be the
problem. When pushing that char array into the list, it is handles as
zero terminated c-string. And every now and then you are missing data.
Just an idea...

Good idea too.

The correct code is

mylist.push_back(std::string(buff, bytesread));

That will make sure that the string pushed onto the list has the correct
length.

If you look at you original code there is nowhere that you have told the
list what the length of your data is, so that should have been a clue
that something was wrong.

john
 
E

emanshu

Hi John,

yes, your comment works for me...
thanks a lot for your help..

Munish Nayyar
emanshu " Innovative MInd"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top