473,407 Members | 2,315 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,407 software developers and data experts.

Binary data in std::list

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

Oct 6 '05 #1
8 3254

"emanshu" <mu*******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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
Oct 6 '05 #2
emanshu wrote:
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

Oct 6 '05 #3
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

Oct 6 '05 #4
emanshu wrote:
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

Oct 6 '05 #5
emanshu wrote:
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

Oct 6 '05 #6
emanshu wrote:
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
Oct 6 '05 #7
Daniel Kay wrote:
emanshu wrote:
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...


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

CU,
Daniel Kay

Oct 6 '05 #8
Hi John,

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

Munish Nayyar
emanshu " Innovative MInd"

Oct 7 '05 #9

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

Similar topics

3
by: Mike Pemberton | last post by:
I'm sure there's a good explanation for this effect, but I get rather a strange output from this little test: #include <iostream> #include <list> int main() { std::list<int> int_list;
3
by: Jef Driesen | last post by:
The number of items in my std::list is changed after sorting (using std::list member function sort). I'm using MSVC6 and the actual (pseudo) code is posted below. The output from size() is...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
3
by: Ray D. | last post by:
Hey all, I'm trying to pass a list into a function to edit it but when I compile using g++ I continue to get the following error: maintainNeighbors.cpp:104: error: invalid initialization of...
12
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.