strange read in of binary file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zl2k

    strange read in of binary file

    hi, there

    I have a appendable binary file of complex data structure named
    data.bin created by myself. It is written in the following format:

    number of Data, Data array

    Suppose I have following data.bin (3 Data appended to 2 Data):

    2, data0, data1, 3, data0, data1, data2

    When I read and display it, I always get the following output:

    2
    data0
    data1
    3
    data0
    data1
    data2
    3
    data with default value
    data with default value
    data with default value

    Why I get an extra data set with default values have the same number
    of Data as the previous valid one? Here is the read in code:

    ---------------------
    ifstream myfile2;
    myfile2.seekg(0 );
    myfile2.open ("data.bin", ios::in | ios::binary);
    int *num2;
    while (myfile2.good() ){
    myfile2.read((c har*)num2, sizeof(int));
    cout<<*num2<<" "<<endl;
    Data *dataArray2 = new Data[*num2];
    myfile2.read ((char*)dataArr ay2, sizeof (Data) * *num2);
    //print dataArray2 content
    delete [] dataArray2;
    }
    myfile2.close() ;
    ---------------------

    Please help!

    zl2k
  • Victor Bazarov

    #2
    Re: strange read in of binary file

    zl2k wrote:
    I have a appendable binary file of complex data structure named
    data.bin created by myself. It is written in the following format:
    >
    number of Data, Data array
    >
    Suppose I have following data.bin (3 Data appended to 2 Data):
    >
    2, data0, data1, 3, data0, data1, data2
    >
    When I read and display it, I always get the following output:
    >
    2
    data0
    data1
    3
    data0
    data1
    data2
    3
    data with default value
    data with default value
    data with default value
    >
    Why I get an extra data set with default values have the same number
    of Data as the previous valid one? Here is the read in code:
    >
    ---------------------
    ifstream myfile2;
    myfile2.seekg(0 );
    myfile2.open ("data.bin", ios::in | ios::binary);
    int *num2;
    while (myfile2.good() ){
    myfile2.read((c har*)num2, sizeof(int));
    cout<<*num2<<" "<<endl;
    Data *dataArray2 = new Data[*num2];
    myfile2.read ((char*)dataArr ay2, sizeof (Data) * *num2);
    //print dataArray2 content
    delete [] dataArray2;
    }
    myfile2.close() ;
    ---------------------
    >
    Please help!
    Common mistake. First, you never check the result of your read
    operations. Change your code to behave appropriately if the file
    cannot be read (where you read your 'num2'): do you really want
    to keep reading the stream if the first read fails? Second, I
    strongly recommend reading the archives on file streams, 'read',
    formatted (or unformatted) I/O, etc. BTW, what book are you
    reading that doesn't explain how to handle input that can run
    out of information (eof conditions)?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • zl2k

      #3
      Re: strange read in of binary file

      On Apr 11, 3:45 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
      zl2k wrote:
      I have a appendable binary file of complex data structure named
      data.bin created by myself. It is written in the following format:
      >
      number of Data, Data array
      >
      Suppose I have following data.bin (3 Data appended to 2 Data):
      >
      2, data0, data1, 3, data0, data1, data2
      >
      When I read and display it, I always get the following output:
      >
      2
      data0
      data1
      3
      data0
      data1
      data2
      3
      data with default value
      data with default value
      data with default value
      >
      Why I get an extra data set with default values have the same number
      of Data as the previous valid one? Here is the read in code:
      >
      ---------------------
      ifstream myfile2;
      myfile2.seekg(0 );
      myfile2.open ("data.bin", ios::in | ios::binary);
      int *num2;
      while (myfile2.good() ){
      myfile2.read((c har*)num2, sizeof(int));
      cout<<*num2<<" "<<endl;
      Data *dataArray2 = new Data[*num2];
      myfile2.read ((char*)dataArr ay2, sizeof (Data) * *num2);
      //print dataArray2 content
      delete [] dataArray2;
      }
      myfile2.close() ;
      ---------------------
      >
      Please help!
      >
      Common mistake. First, you never check the result of your read
      operations. Change your code to behave appropriately if the file
      cannot be read (where you read your 'num2'): do you really want
      to keep reading the stream if the first read fails? Second, I
      strongly recommend reading the archives on file streams, 'read',
      formatted (or unformatted) I/O, etc. BTW, what book are you
      reading that doesn't explain how to handle input that can run
      out of information (eof conditions)?
      >
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask
      I tried while (!myfile2.eof() ){} but makes no difference. How can I
      fix it? I know my code did not stop when reached the end of the file.
      zl2k

      Comment

      • Victor Bazarov

        #4
        Re: strange read in of binary file

        zl2k wrote:
        On Apr 11, 3:45 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        >zl2k wrote:
        >>I have a appendable binary file of complex data structure named
        >>data.bin created by myself. It is written in the following format:
        >>
        >>number of Data, Data array
        >>
        >>Suppose I have following data.bin (3 Data appended to 2 Data):
        >>
        >>2, data0, data1, 3, data0, data1, data2
        >>
        >>When I read and display it, I always get the following output:
        >>
        >>2
        >>data0
        >>data1
        >>3
        >>data0
        >>data1
        >>data2
        >>3
        >>data with default value
        >>data with default value
        >>data with default value
        >>
        >>Why I get an extra data set with default values have the same number
        >>of Data as the previous valid one? Here is the read in code:
        >>
        >>---------------------
        >> ifstream myfile2;
        >>myfile2.seekg (0);
        >>myfile2.ope n ("data.bin", ios::in | ios::binary);
        >>int *num2;
        >>while (myfile2.good() ){
        >>myfile2.read( (char*)num2, sizeof(int));
        >>cout<<*num2<< " "<<endl;
        >>Data *dataArray2 = new Data[*num2];
        >>myfile2.rea d ((char*)dataArr ay2, sizeof (Data) * *num2);
        >>//print dataArray2 content
        >>delete [] dataArray2;
        >>}
        >>myfile2.close ();
        >>---------------------
        >>
        >>Please help!
        >>
        >Common mistake. First, you never check the result of your read
        >operations. Change your code to behave appropriately if the file
        >cannot be read (where you read your 'num2'): do you really want
        >to keep reading the stream if the first read fails? Second, I
        >strongly recommend reading the archives on file streams, 'read',
        >formatted (or unformatted) I/O, etc. BTW, what book are you
        >reading that doesn't explain how to handle input that can run
        >out of information (eof conditions)?
        >>
        >V
        >--
        >Please remove capital 'A's when replying by e-mail
        >I do not respond to top-posted replies, please don't ask
        >
        I tried while (!myfile2.eof() ){} but makes no difference. How can I
        fix it? I know my code did not stop when reached the end of the file.
        zl2k
        Look at what 'read' returns. You can also check the condition of the
        stream right after 'read' to see whether the stream is "good". You
        can check for 'eof' instead of "good" after 'read'...

        I don't want to write your program for you. Trust me, you'll be much
        better off if you research this stuff yourself.

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask


        Comment

        • zl2k

          #5
          Re: strange read in of binary file

          On Apr 11, 4:40 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
          zl2k wrote:
          On Apr 11, 3:45 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
          zl2k wrote:
          >I have a appendable binary file of complex data structure named
          >data.bin created by myself. It is written in the following format:
          >
          >number of Data, Data array
          >
          >Suppose I have following data.bin (3 Data appended to 2 Data):
          >
          >2, data0, data1, 3, data0, data1, data2
          >
          >When I read and display it, I always get the following output:
          >
          >2
          >data0
          >data1
          >3
          >data0
          >data1
          >data2
          >3
          >data with default value
          >data with default value
          >data with default value
          >
          >Why I get an extra data set with default values have the same number
          >of Data as the previous valid one? Here is the read in code:
          >
          >---------------------
          > ifstream myfile2;
          >myfile2.seekg( 0);
          >myfile2.open ("data.bin", ios::in | ios::binary);
          >int *num2;
          >while (myfile2.good() ){
          >myfile2.read(( char*)num2, sizeof(int));
          >cout<<*num2< <" "<<endl;
          >Data *dataArray2 = new Data[*num2];
          >myfile2.read ((char*)dataArr ay2, sizeof (Data) * *num2);
          >//print dataArray2 content
          >delete [] dataArray2;
          >}
          >myfile2.close( );
          >---------------------
          >
          >Please help!
          >
          Common mistake. First, you never check the result of your read
          operations. Change your code to behave appropriately if the file
          cannot be read (where you read your 'num2'): do you really want
          to keep reading the stream if the first read fails? Second, I
          strongly recommend reading the archives on file streams, 'read',
          formatted (or unformatted) I/O, etc. BTW, what book are you
          reading that doesn't explain how to handle input that can run
          out of information (eof conditions)?
          >
          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask
          >
          I tried while (!myfile2.eof() ){} but makes no difference. How can I
          fix it? I know my code did not stop when reached the end of the file.
          zl2k
          >
          Look at what 'read' returns. You can also check the condition of the
          stream right after 'read' to see whether the stream is "good". You
          can check for 'eof' instead of "good" after 'read'...
          >
          I don't want to write your program for you. Trust me, you'll be much
          better off if you research this stuff yourself.
          >
          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask
          Thanks, I can jump out off the loop by checking the good() or eof()
          after myfile2.read((c har*)num2, sizeof(int));
          So I can move one. Have good weekend.
          zl2k

          Comment

          Working...