This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libstdc++/13333] New: [cygwin] fstream tell/seek bug with DOS-format text files


Cygwin and MinGW GCC 3.3.1 have serious problems with tell/seek operations on 
DOS (\r\n line-ending) text files. This problem is also present in GCC 3.2.3 
but I believe it is a regression from earlier versions but I cannot be 
certain.  I have seen a few symptoms of this problem that I believe are 
related.  The problem can cause corruption of files on which only seek and read 
operations are performed. Two example cases are provided below.


Example 1.  Using seekg with a tellg value doesn't return to the correct point 
in the file.  (It works properly with *nix text files on Cygwin and MinGW.)  
The seekg should return to the beginning of the second line but it doesn't.

// seek.cc
//
// Build with g++ seek.cc -o seek.exe 

#include <string>
#include <iostream>
#include <fstream>


int
main()
{
   std::ifstream afile( "sample.txt" );
   std::string inp_line;
   getline( afile, inp_line );
   std::cout << inp_line << std::endl;
   std::streampos sp = afile.tellg(); // Start of 2nd line
   getline( afile, inp_line );
   std::cout << inp_line << std::endl;
   getline( afile, inp_line );
   std::cout << inp_line << std::endl;
   afile.seekg( sp ); // Return to start of 2nd line
   getline( afile, inp_line );
   std::cout << inp_line << std::endl;
   afile.close();
   return 0;
}


/* sample.txt File (DOS line endings)
first line
second line
third line
fourth line
*/


/* Output generated by Cygwin and MinGW GCC 3.3.1.
first line
second line
third line
ond line
*/


Example 2. Using seekg followed by a read can corrupt a DOS text file.

#include <fstream>
#include <string>


int
main()
{
	std::fstream score_stream( "stream.out", 
std::ios::in|std::ios::out|std::ios::trunc );
	score_stream << "filename\n";

	score_stream.seekg( std::ios::beg );
	std::string str;
	score_stream >> str;

	score_stream.close();

	return 0;
}

/* stream.out contents after running stream.exe (notice the extra 'f'):
ffilename
*/


I tried submitting this to MinGW Bugzilla but it was bounced as a GCC problem.

Platform: Windows 2000 SP2, dual PIII, RAID0 SCSI disks

-- 
           Summary: [cygwin] fstream tell/seek bug with DOS-format text
                    files
           Product: gcc
           Version: 3.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: sgm at objexx dot com
                CC: gcc-bugs at gcc dot gnu dot org
 GCC build triplet: i686-pc-cygwin
  GCC host triplet: i686-pc-cygwin
GCC target triplet: i686-pc-cygwin


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13333


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]