Trouble with ofstream::open in VC++4.2 
Author Message
 Trouble with ofstream::open in VC++4.2

Hi there,

I'm having trouble with the output file stream write method in Visual
C++ 4.2. I'm trying to write out a binary file from a char* but the
character 0xFF isn' t written out at all.

Consider the following code:

void main () {
  fstream file;
  file.open( "test.tmp", ios::out | ios::trunc | ios::binary );
  if ( file.fail() ) {
    cerr << "File open failed!\n";
    return;
  };
  char buffer[16] = {
    '\377', '\001', '\002', '\003', '\377', '\005', '\006', '\007',
    '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017'
  };
  file.write( buffer, 16);
  if ( file.fail() ) {
    cerr << "File write failed!\n";
  };
  // this works!
  //for ( int i=0; i<16; i++ )
  //file.put( buffer[i] );
  file.close();

Quote:
};

The resulting file will be:

000000  01 02 03 05 06 07 08 09 0A 0B 0C 0D 0E 0F

So all '\377', i.e. 0xFF are lost. I've seen that EOF is coded as -1,
so the 0xFF is probably interpreted as end-of-file. I've compiled this
under UNIX and it works fine. Is this a known bug in VC++4.2 or am I
doing something wrong?

The workaround with 'put' is fine, but seems slower. Can somebody show
me a way using 'write' or a different method that can write chunks of
data into a file stream.

Thanks,
Jens



Sun, 01 Apr 2001 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. ofstream vs. fwrite trouble

2. OFSTREAM explicity opening stdout?

3. ofstream.open crashing

4. OFSTREAM - Opening a file name equivalent to COUT

5. OFSTREAM open error, help!

6. Open a file with std::ofstream without truncate it

7. Control sharing for files opened in a std::ofstream object

8. How to open a ofstream object for append?

9. OFSTREAM - Opening a file name equivalent to COUT

10. ofstream default open inconsistent

11. File Open function trouble....

12. Trouble opening a file

 

 
Powered by phpBB® Forum Software