This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
fstream problems
- From: "Phyllis Tedford" <ptedford at sci dot tamucc dot edu>
- To: <gcc-help at gcc dot gnu dot org>
- Date: Mon, 28 Aug 2006 12:51:11 -0500
- Subject: fstream problems
Greetings.
I'm not sure I'm sending my question/problem to the right place. If not,
maybe you can suggest who I might contact. Anyway...
One of my colleagues found a problem with a sample program I had posted in
my lecture notes. The program involved using an fstream object for both
input and output to/from a file. Here's the relevant chunk of code:
fstream aFile;
aFile.open(?data.txt?, ios::in | ios::out);
if (!aFile)
{
cout << ?Oops! Didn?t open!?;
exit(0);
}
aNewLine = ?A new record.?; //aNewLine and oneLine are string class vars
aFile << aNewLine << ?\n?;
getline(aFile, oneLine);
while (aFile)
{
cout << oneLine << ?\n?;
getline(aFile, oneLine);
}
aFile.close();
The "data.txt" file contains:
Record 1
Record 2
Record 3
Record 4
When I/we run the program as written, nothing is displayed. In fact, it
never goes into the "while" loop (even though the file stream is still
"good" - I checked). After the program executes, the file contains the
expected:
A new record
d 2
Record 3
Record 4
After mucking about with various debugging attempts, I finally tried using
"tellg" and "tellp" to find out where the read/write marker was. Here's the
modified code (sans opening the file):
aNewLine = "A new record.";
aFile << aNewLine << '\n';
aFile.tellp();
aFile.tellg();
getline(aFile, oneLine);
while (aFile)
{
cout << oneLine << '\n';
getline(aFile, oneLine);
}
This gave the results the original program was supposed to!! My question is
"Why?!"
You might need to know that we're running a gnu compiler (v. 3.4.5) on a
UNIX server. Prior to some upgrades and such that were done about 10 days
ago, the original program ran just fine. Any input/suggestions/explanations
you might provide would be greatly appreciated.
Thanks.
Regards,
Phyllis Tedford
Instructor - Computer Science
Computing and Math Sciences Dept.
College of Science & Technology
Texas A&M University - Corpus Christi
6300 Ocean Drive
Corpus Christi, TX 78412
Ph: (361) 825-3711