Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

major getline() error

Status
Not open for further replies.

fugigoose

Programmer
Jun 20, 2001
241
US
I'm trying to read a file a line at a time and store it in a string using the getline() function. This is the troubled section:

Code:
int cls_console::execScriptFile(const char* fileName){
int r;
string lin;
string strmsg;
string strfil;
ifstream scr(fileName);
strmsg = "cls_console.execScriptFile::Executing script file ";
strfil = fileName;
strmsg = strmsg + strfil;
logMessage(strmsg.c_str());
if (scr){
while(!scr.eof()){
getline(scr, lin);
r = doCommand(lin.c_str());
if (r) logMessage(getErrorString(r));
}
}else{
return 2;
}
logMessage("cls_console.execScriptFile::Finished");
return 0;
}

The getline code makes the compiler have a debug assertion error in file fopen.c at line 53. It says the expression is 'file != NULL'. I can't even find fopen.c, it doesnt exist as far as i can tell. It must be generated or extracted at compile time. Why isn't my getline function working? Btw, here are the includes:

Code:
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
 
> The getline code makes the compiler have a debug assertion error in file fopen.c at line 53
Can you clarify what the problem is, and when it is occuring.

Assertion failures happen when you run debug code, so they tend to be spotted by the debugger, not the compiler.

Also, the fopen.c suggests that the problem is with opening the file in the first place, not reading it.
> ifstream scr(fileName);
It it really is this line, then you passed a value of NULL in fileName.
If you really do mean the getline is failing, then something else is definitely going on.

> while(!scr.eof()){
eof() is a record of past events, not a prediction of future events. If your file contains 1 line, and you call getline once, then eof() will still be false. You have to call getline again in order to make eof() true.
But you don't detect the failed call to getline.
A frequent associated question is "why does this code process the last line twice".

The usual way is
[tt]while ( getline(scr, lin) ) {
// do stuff
}[/tt]

--
 
I changed the while loop to your standards, since it makes a lot more since. However the debug assertion failure persists. I'm not sure if its the actual opening of the file however. If i debug, fileName isn't null, it's 'system\\init.txt'. Also, by means of using break points, the debugger doesn't fail untill it gets to the 'getline()'. The opening of the file seems to work fine. What doesnt make sense is that my original code worked for awhile, then it stopped, possibly after i made modifications to the 'init.txt' file. I've tried re-typing that file too, but to no avail. This is a big roadblock in my engine developement, i need to be able to read script files. Thanx.
 
Copy this function (and a minimal amount of support code, like LogMessage and DoCommand would be just a cout) into another project and test it in isolation.

My suspicion is that the problem is really elsewhere in your code, and the problem is only being noticed here (the effect). The root cause is elsewhere.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top