Problem with getline and files

G

Guest

Hi!
I'm having trouble reading text lines from a file describing the levels
for a simple game I'm creating.

Inside the function that reads the content of the file there is a loop
that basically looks like this

ifstream file("levels\\level1.txt");
string line;
getline(file, line);
while(!file.eof()){
...
getline(file, line);
}

The first part of the level file looks like this

#configs
levels/objectconfig.txt
#mappings
....

I've tried reading the content of the file using a test application I
wrote just to try to find the bug but I get the same problem. When it's
time for the program to read the #mappings line the debugger displays
the content of the line variables as ??? or a square symbol. If I
remove the path line in the middle of the two lines above everyting
works ok. I've tried using \\ and \ instead of / but that didn't help.

The strange thing is that if I unroll the loop and create something
like

ifstream file("levels\\level1.txt");
string line;
getline(file, line);
getline(file, line);

string line2;
getline(file, line2);

line2 will contain #mappings after the call to getline.

I'm using Visual Studio .NET 2003.

Any ideas?

/M
 
D

David Harmon

On 1 Jan 2006 09:40:29 -0800 in comp.lang.c++,
(e-mail address removed) wrote,
getline(file, line);
while(!file.eof()){

Should be:
while(getline(file,line)) {
time for the program to read the #mappings line the debugger displays
the content of the line variables as ??? or a square symbol. If I

The smarter a debugger is, the less I trust it. I would rather
trust "cerr <<" in the program.
remove the path line in the middle of the two lines above everyting
works ok. I've tried using \\ and \ instead of / but that didn't help.

There should be nothing special about / or \ in file data.
As you are aware, \ is special between quotes in source code.
 
P

Peter Jansson

Hi!
I'm having trouble reading text lines from a file describing the levels
for a simple game I'm creating.

Inside the function that reads the content of the file there is a loop
that basically looks like this

ifstream file("levels\\level1.txt");
string line;
getline(file, line);
while(!file.eof()){
...
getline(file, line);
}

The first part of the level file looks like this

#configs
levels/objectconfig.txt
#mappings
....

I've tried reading the content of the file using a test application I
wrote just to try to find the bug but I get the same problem. When it's
time for the program to read the #mappings line the debugger displays
the content of the line variables as ??? or a square symbol. If I
remove the path line in the middle of the two lines above everyting
works ok. I've tried using \\ and \ instead of / but that didn't help.

The strange thing is that if I unroll the loop and create something
like

ifstream file("levels\\level1.txt");
string line;
getline(file, line);
getline(file, line);

string line2;
getline(file, line2);

line2 will contain #mappings after the call to getline.

I'm using Visual Studio .NET 2003.

Any ideas?

/M

Try this:

std::ifstream file("levels\\level1.txt");
std::string line;
while(std::getline(file, line)){
// Do stuff with line...
}

Regards,
Peter Jansson
 
A

Andrew Koenig

Inside the function that reads the content of the file there is a loop
that basically looks like this

ifstream file("levels\\level1.txt");
string line;
getline(file, line);
while(!file.eof()){
...
getline(file, line);
}

Here's a nice idiom for this kind of thing, which I learned from Walter
Brown:

ifstream file("levels\\level1.txt");
for (string line; getline(file, line); ) {
// whatever
}
 
M

Marcus Kwok

Andrew Koenig said:
Inside the function that reads the content of the file there is a loop
that basically looks like this

ifstream file("levels\\level1.txt");
string line;
getline(file, line);
while(!file.eof()){
...
getline(file, line);
}

Here's a nice idiom for this kind of thing, which I learned from Walter
Brown:

ifstream file("levels\\level1.txt");
for (string line; getline(file, line); ) {
// whatever
}[/QUOTE]

That is nice. I like that better than my usual idiom of

ifstream file("levels\\level1.txt");
string line;
while (getline(file, line)) {
// whatever
}

since it automatically makes line go out of scope when it's finished.
 
R

Roland Pibinger

That is nice. I like that better than my usual idiom of

ifstream file("levels\\level1.txt");
string line;
while (getline(file, line)) {
// whatever
}

Questions like the above frequently pop up in C++ newsgroups. People
don't understand what e.g. 'while (getline(file, line))' or 'while
(cin >> str)' means. The problem is that iostreams fail to provide
intuituive and consistent error handling (therefore error handling is
usually neglected in examples). A clearer 'idiom' would be:

while (getline(file, line) != NULL) {
// ...
}
if (!file.eof() || file.bad()) {
// error
}

Best wishes,
Roland Pibinger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

problem with getline 6
ifstream::getline() synatx 18
getline questions 2
getline problems 8
getline and deleteline 1
Problem with KMKfw libraries 1
problem with getline 1
Stringstreams Getline 3

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top