egcs-1.1.1 - read of istream with ios::bin doesnt work?

Johann Petrak johann@ai.univie.ac.at
Fri Dec 3 00:48:00 GMT 1999


Sorry, here is a complete program:

// Snip - Begin program test1.cc
#include<fstream>
#include<cstdlib>

void clrbuf(char * buf) { for(int i=0;i<20;i++) buf[i]=0; }

main () {
  ofstream out ("test1.file",ios::bin);
  out.write("0123456789",10);
  out.close();

  char buf[20];
  cout << "Binary mode:\n";
  clrbuf(buf);
  ifstream in1 ("test1.file",ios::bin);
  in1.read(buf,10);
  cout << ">" << buf << "< " << in1.gcount() << endl;
  in1.close();

  cout << "Text mode:\n";
  clrbuf(buf);
  ifstream in2("test1.file");
  in2.read(buf,10);
  cout << ">" << buf << "< " << in2.gcount() << endl;
  in2.close();
}
// Snap - End program test1.cc 

Compiled with:
  Reading specs from
/usr/local/lib/gcc-lib/sparc-sun-solaris2.5.1/egcs-2.91.60/specs
  gcc version egcs-2.91.60 19981201 (egcs-1.1.1 release)

Here is the program's output for this compiler:

Binary mode:
>< 0
Text mode:
>1234567890< 10

The file test1.file was written correctly:
% od -c test1.file
0000000   1   2   3   4   5   6   7   8   9   0
0000012

Compiled with cygwins gcc:
  gcc version egcs-2.91.57 19980901 (egcs-1.1 release) 
  
produces same erroneous output as above.

Classic C-style binary I/O (fopen(..,"rb") works,
so I rewrote my code to use it for now.

Cheers,
  Johann


"Martin v. Loewis" wrote:
> 
> > I wonder why
> >     ifstream in ("test1.file",ios::bin);
> >     in.read(buffer,10);
> > will read in 0 characters (this is also returned
> > by in.gcount()), while
> >   ifstream in ("test1.file");
> >   in.read(&buf,10);
> > will work just OK.
> 
> Thanks for your bug report. Unfortunately, you provide too little
> information to reproduce the bug. What is buffer? What is buf? What is
> test1.file? Please give complete source.
> 
> Thanks,
> Martin


More information about the Gcc-bugs mailing list