FIX: ifstream Object Loses Character in Text Mode (102158)



The information in this article applies to:

  • Microsoft Visual C++ for Windows, 16-bit edition 1.0
  • Microsoft Visual C++ for Windows, 16-bit edition 1.5
  • Microsoft Visual C++, 32-bit Editions 1.0
  • Microsoft Visual C++, 32-bit Editions 2.0
  • Microsoft Visual C++, 32-bit Editions 4.0
  • Microsoft Visual C++, 32-bit Editions 4.1
  • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
  • Microsoft Visual C++, 32-bit Professional Edition 4.2

This article was previously published under Q102158

SYMPTOMS

When an application reads characters into an ifstream object opened in text mode, data may be lost and not placed into the buffer. The code does not provide any warning or other indication of this data loss.

STATUS

Microsoft has confirmed this to be a problem in the products listed at the beginning of this article. This problem was fixed in Visual C++ 5.0.

MORE INFORMATION

The following code example demonstrates this problem by filling a buffer with the character sequence "01234567890..." and using an ifstream object to copy the data to another buffer. The transferred sequence reads as "013456..." Only the character "2" is missing from the sequence. The remainder of the sequence transfers correctly.

Sample Code

/*
 * Compiler options needed:  -GX for 5.0 only
 */ 

#include <iostream.h>
#include <fstream.h>

// To resolve the problem using Visual C++ 5.0, use the
// following three statements instead of the preceding two:
// 
// #include <iostream>
// #include <fstream>
// using namespace std;


void main(void)
{
   ofstream os("test.tmp");
   for (int i = 0; i < 512; i++)
   {
      os.put((char)('0' + i % 10));
   }
   os.put('\n');
   os.close();

   streampos pos;
   ifstream is("test.tmp");
// To resolve this error using Visual C++ 4.x and earlier, use the
// following line to open the ifstream instead of the line above.
// ifstream is("test.tmp", ios::binary);
   for (int k = 0; k < 10; k++)
   {
      pos = is.tellg();
      cout << (char)is.get();
   }
}
				

Modification Type:MinorLast Reviewed:7/5/2005
Keywords:kbbug kbcode kbCompiler kbCPPonly kbfix kbVC500fix KB102158