This is the mail archive of the gcc-prs@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]

libstdc++/7172: fstream object cannot be read from a second file after first file is closed


>Number:         7172
>Category:       libstdc++
>Synopsis:       fstream object cannot be read from a second file after first file is closed
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Sun Jun 30 19:06:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Sarat Chandra Maruvada
>Release:        gcc version 3.1
>Organization:
>Environment:
Debian GNU/Linux,PC
>Description:
The code reads the no. of bytes in the first file.the file object is closed and then associated with a second file. the second file is opened but the first byte is recognized as a EOF which is false.The output of second statement is 0 which is the wrong answer.This code works well on sparc solaris with gcc version egcs-2.91.60.
>How-To-Repeat:
create 2 test files:
File1 : tst1
hello world

File2: tst2
bye world

run the compiled code in the directory with the two text files 'tst1' and 'tst2'.the first file's byte count is returned but not the second.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="test2.cc"
Content-Disposition: inline; filename="test2.cc"

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

using namespace std;

main()
{
	string fName="tst1";
	ifstream IN(fName.c_str());
	if (IN.is_open()==false)
	{
		cout<<"File not open!!"<<endl;
		exit(-1);
	}
	int count=0;
	while (IN.eof()!=true)
	{
		IN.get();
		count++;
	}
	IN.seekg(0,ios::beg);
	IN.close();
	cout<<"Number of bytes are:"<<count<<endl;
	
	//ifstream IN;
	fName="tst2";
	IN.open(fName.c_str(),ios::in | ios::binary);
	if (IN.is_open()==false)
	{
		cout<<"File not open!!"<<endl;
		exit(-1);
	}
	count=0;
	IN.seekg(0,ios::beg);
	while (IN.eof()!=true)
	{
		IN.get();
		count++;
	}
	IN.close();
	cout<<"Number of bytes are:"<<count<<endl;
}


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