S
Sanyi
I am having some trouble rereading a file for input. Here's the
problem: I have a text file with some data (football scores) in its
lines. First I read them line-by line to figure out how many lines it
has. Afterwards I want to read them again, and this time do some
processing. However I cannot make my program do this. I tried the
following:
############################
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inGameFile;
inGameFile.open( "scores.txt", ios::in );
if ( !inGameFile)
{
cerr << "Failed to open(1).\n";
exit(1);
}
const int LINE = 100;
char stLine[LINE+1];
// count the number of games
int nGames = 0;
while ( inGameFile.getline(stLine, LINE) )
{
nGames++;
}
cout << "A total of " << nGames << " games played.\n";
// Everything works fine this far
if ( !inGameFile)
{
cerr << "No longer open(2).\n"; // inGameFile no longer defined!
//exit(1);
}
cout << inGameFile.tellg() << endl; // result is -1
inGameFile.seekg( 0, ios::beg );
cout << inGameFile.tellg() << endl; // result is -1 again!
// Does not work this way; try to close and reopen:
inGameFile.close();
if ( !inGameFile)
{
cerr << "Failed to close(3).\n";
//exit(1);
}
inGameFile.open( "scores.txt", ios::in );
if ( !inGameFile)
{
cerr << "Failed to open(4).\n";
//exit(1);
} inGameFile.open( "scores.txt", ios::in );
}
############################
The output is:
A total of 1363 games played.
No longer open(2).
-1
-1
Failed to close(3).
Failed to open(4).
############################
Compiler is c++ (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
I did something similar on gnu c++ 2.95 under windows, where I could
resolve it by closing and reopening the file. This time it does not
work.
It seems that my inGameFile is automatically closed when I read the
last line, and I can neither move the get pointer nor re-open the
file.
Can someone explain me how can I overcome this?
Thanks,
Sanyi
problem: I have a text file with some data (football scores) in its
lines. First I read them line-by line to figure out how many lines it
has. Afterwards I want to read them again, and this time do some
processing. However I cannot make my program do this. I tried the
following:
############################
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inGameFile;
inGameFile.open( "scores.txt", ios::in );
if ( !inGameFile)
{
cerr << "Failed to open(1).\n";
exit(1);
}
const int LINE = 100;
char stLine[LINE+1];
// count the number of games
int nGames = 0;
while ( inGameFile.getline(stLine, LINE) )
{
nGames++;
}
cout << "A total of " << nGames << " games played.\n";
// Everything works fine this far
if ( !inGameFile)
{
cerr << "No longer open(2).\n"; // inGameFile no longer defined!
//exit(1);
}
cout << inGameFile.tellg() << endl; // result is -1
inGameFile.seekg( 0, ios::beg );
cout << inGameFile.tellg() << endl; // result is -1 again!
// Does not work this way; try to close and reopen:
inGameFile.close();
if ( !inGameFile)
{
cerr << "Failed to close(3).\n";
//exit(1);
}
inGameFile.open( "scores.txt", ios::in );
if ( !inGameFile)
{
cerr << "Failed to open(4).\n";
//exit(1);
} inGameFile.open( "scores.txt", ios::in );
}
############################
The output is:
A total of 1363 games played.
No longer open(2).
-1
-1
Failed to close(3).
Failed to open(4).
############################
Compiler is c++ (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
I did something similar on gnu c++ 2.95 under windows, where I could
resolve it by closing and reopening the file. This time it does not
work.
It seems that my inGameFile is automatically closed when I read the
last line, and I can neither move the get pointer nor re-open the
file.
Can someone explain me how can I overcome this?
Thanks,
Sanyi