Binary data in std::list

Discussion in 'C++' started by emanshu, Oct 6, 2005.

  1. emanshu

    emanshu Guest

    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
     
    emanshu, Oct 6, 2005
    #1
    1. Advertising

  2. emanshu

    mlimber Guest

    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
     
    mlimber, Oct 6, 2005
    #2
    1. Advertising

  3. emanshu

    Mike Wahler Guest

    "emanshu" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Oct 6, 2005
    #3
  4. emanshu

    emanshu Guest

    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
     
    emanshu, Oct 6, 2005
    #4
  5. emanshu

    mlimber Guest

    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
     
    mlimber, Oct 6, 2005
    #5
  6. emanshu

    mlimber Guest

    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
     
    mlimber, Oct 6, 2005
    #6
  7. emanshu

    Daniel Kay Guest

    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
     
    Daniel Kay, Oct 6, 2005
    #7
  8. 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
     
    John Harrison, Oct 6, 2005
    #8
  9. emanshu

    emanshu Guest

    Hi John,

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

    Munish Nayyar
    emanshu " Innovative MInd"
     
    emanshu, Oct 7, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,350
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    376
    Jim Langston
    Jul 7, 2005
  3. Replies:
    6
    Views:
    662
    Jim Langston
    Oct 30, 2005
  4. Juha Nieminen
    Replies:
    22
    Views:
    1,048
    Kai-Uwe Bux
    Oct 12, 2007
  5. MM
    Replies:
    98
    Views:
    2,397
    Keith H Duggar
    Feb 16, 2011
Loading...

Share This Page