Hi,
since I've gotten prompt and correct information here before, here goes 
again:

In porting octave I've come across a problem with iostream. Reading 
from a file of binary data, the last byte gets lost. It seems that the 
stream is invalidated when reading up to EOF rather than actually 
reading EOF.
Running the code snippet below will show what I mean (the file bindata 
contains "foo-i-hithere\n", 14 chars):

% cat bindata
foo-i-hithere
% wc bindata
        1       1      14 bindata
% g++ -o readtest readtest.cc
% ./readtest
is before: 0xbffffb60, is after: 0xbffffb60, char 1: 'f'
is before: 0xbffffb60, is after: 0xbffffb60, char 2: 'o'
is before: 0xbffffb60, is after: 0xbffffb60, char 3: 'o'
is before: 0xbffffb60, is after: 0xbffffb60, char 4: '-'
is before: 0xbffffb60, is after: 0xbffffb60, char 5: 'i'
is before: 0xbffffb60, is after: 0xbffffb60, char 6: '-'
is before: 0xbffffb60, is after: 0xbffffb60, char 7: 'h'
is before: 0xbffffb60, is after: 0xbffffb60, char 8: 'i'
is before: 0xbffffb60, is after: 0xbffffb60, char 9: 't'
is before: 0xbffffb60, is after: 0xbffffb60, char 10: 'h'
is before: 0xbffffb60, is after: 0xbffffb60, char 11: 'e'
is before: 0xbffffb60, is after: 0xbffffb60, char 12: 'r'
is before: 0xbffffb60, is after: 0xbffffb60, char 13: 'e'
is before: 0xbffffb60, is after: 0, EOF
total characters read: 13

The expected output was (from a Linux system):
$ cat bindata
foo-i-hithere
$ wc bindata
       1       1      14 bindata
$ g++ readtest.cc
$ ./a.out
is before: 0xffffffff, is after: 0xffffffff, char 1: 'f'
is before: 0xffffffff, is after: 0xffffffff, char 2: 'o'
is before: 0xffffffff, is after: 0xffffffff, char 3: 'o'
is before: 0xffffffff, is after: 0xffffffff, char 4: '-'
is before: 0xffffffff, is after: 0xffffffff, char 5: 'i'
is before: 0xffffffff, is after: 0xffffffff, char 6: '-'
is before: 0xffffffff, is after: 0xffffffff, char 7: 'h'
is before: 0xffffffff, is after: 0xffffffff, char 8: 'i'
is before: 0xffffffff, is after: 0xffffffff, char 9: 't'
is before: 0xffffffff, is after: 0xffffffff, char 10: 'h'
is before: 0xffffffff, is after: 0xffffffff, char 11: 'e'
is before: 0xffffffff, is after: 0xffffffff, char 12: 'r'
is before: 0xffffffff, is after: 0xffffffff, char 13: 'e'
is before: 0xffffffff, is after: 0xffffffff, char 14: '
'
is before: 0xffffffff, is after: (nil), EOF
total characters read: 14

I'm not sure who's right here, but eitherway we'll have to find a way 
for the code to work on all systems.

Suggestions?
/Per

file: readtest.cc
---------
#include <iostream>
#include <fstream>

using namespace std;

int
main (void)
{
   int count = 0;

   ifstream is ("bindata");

   if (is)
     {
       for (;;)
         {
           char tmp;

           cout << "is before: " << is;

           is.read (&tmp, sizeof (char));

           cout << ", is after: " << is;

           if (is)
             cout << ", char " << ++count << ": '" << tmp << "'\n";
           else
             {
               if (is.eof ())
                 cout << ", EOF\n";
               else
                 cout << "read error\n";

               break;
             }
         }
       is.close ();
     }
   else
     cout << "error opening input file\n";

   cout << "total characters read: " << count << "\n";

   return count;
}
----------



------------
Per Persson
Blekinge Institute of Technology
Dept. of Signal Processing and Telecommunications

www:   http://www.its.bth.se/staff/pee
e-mail: [EMAIL PROTECTED]

Reply via email to