By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,042 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,042 IT Pros & Developers. It's quick & easy.

Problem with getline and files

P: n/a
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

Jan 1 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On 1 Jan 2006 09:40:29 -0800 in comp.lang.c++,
al**************@hotmail.com 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.

Jan 1 '06 #2

P: n/a
al**************@hotmail.com wrote:
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
Jan 1 '06 #3

P: n/a
<al**************@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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
}
Jan 2 '06 #4

P: n/a
Andrew Koenig <ar*@acm.org> wrote:
<al**************@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
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
}


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.

--
Marcus Kwok
Jan 6 '06 #5

P: n/a
On Fri, 6 Jan 2006 18:43:19 +0000 (UTC), ri******@gehennom.net (Marcus
Kwok) wrote:
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

Jan 6 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.