Reading bytes from binary files containing '0' character

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

    #1

    Reading bytes from binary files containing '0' character

    Hi,
    I am writing this application in C++.
    It reads data from binary files.
    My current requirement is that: Given a positive number N, I have to
    read in N bytes from the input stream (which is from a binary file ).

    My code fragment looks as follows:

    void MyStream ::readStream(ch ar * buffer, size_t length) {
    is.get( buffer, length + 1); //read length bytes from the stream
    // 'is' -> field of type istream .

    }

    The invocation fragment looks as follows:

    char * buffer = new char[length + 1];
    readStream(buff er, length);


    The problem occurs when the binary stream contains a null character in
    it.
    Suppose, if I try to read 10 characters from the stream, and the fourth
    character happens to be a null character (i.e 0 ) , then only 3
    characters
    are read. I am looking for a work-around to read 10 characters i.e read
    past this null character here. Is there any workaround to this ?

  • Mole Wang

    #2
    Re: Reading bytes from binary files containing '0' character

    The problem in your code is not caused by the stream but by the C-style string.
    The "get" function does read "length" byte of data into the buffer if the input
    stream doesnot meet any error or arrive at the end of file during the read operation.
    When a null charactor occurs in your C-style string, it just look like to be
    truncated. You just look the string to be a byte array.

    "Rakesh Sinha" <rakesh_usenet@ yahoo.com> wrote in message news:1104209189 .112742.244250@ z14g2000cwz.goo glegroups.com.. .[color=blue]
    > Hi,
    > I am writing this application in C++.
    > It reads data from binary files.
    > My current requirement is that: Given a positive number N, I have to
    > read in N bytes from the input stream (which is from a binary file ).
    >
    > My code fragment looks as follows:
    >
    > void MyStream ::readStream(ch ar * buffer, size_t length) {
    > is.get( buffer, length + 1); file://read length bytes from the stream
    > // 'is' -> field of type istream .
    >
    > }
    >
    > The invocation fragment looks as follows:
    >
    > char * buffer = new char[length + 1];
    > readStream(buff er, length);
    >
    >
    > The problem occurs when the binary stream contains a null character in
    > it.
    > Suppose, if I try to read 10 characters from the stream, and the fourth
    > character happens to be a null character (i.e 0 ) , then only 3
    > characters
    > are read. I am looking for a work-around to read 10 characters i.e read
    > past this null character here. Is there any workaround to this ?
    >[/color]


    Comment

    • Jonathan Mcdougall

      #3
      Re: Reading bytes from binary files containing '0' character

      Rakesh Sinha wrote:[color=blue]
      > Hi,
      > I am writing this application in C++.
      > It reads data from binary files.
      > My current requirement is that: Given a positive number N, I have to
      > read in N bytes from the input stream (which is from a binary file ).
      >
      > My code fragment looks as follows:
      >
      > void MyStream ::readStream(ch ar * buffer, size_t length) {
      > is.get( buffer, length + 1); //read length bytes from the stream
      > // 'is' -> field of type istream .
      >
      > }
      >
      > The invocation fragment looks as follows:
      >
      > char * buffer = new char[length + 1];
      > readStream(buff er, length);
      >
      >
      > The problem occurs when the binary stream contains a null character in
      > it.
      > Suppose, if I try to read 10 characters from the stream, and the fourth
      > character happens to be a null character (i.e 0 ) , then only 3
      > characters
      > are read. I am looking for a work-around to read 10 characters i.e read
      > past this null character here. Is there any workaround to this ?[/color]

      Use std::istream::r ead().


      Jonathan

      Comment

      Working...