Connecting Tech Pros Worldwide Help | Site Map

Problem with getline and files

allspamgoeshere3@hotmail.com
Guest
 
Posts: n/a
#1: Jan 1 '06
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

David Harmon
Guest
 
Posts: n/a
#2: Jan 1 '06

re: Problem with getline and files


On 1 Jan 2006 09:40:29 -0800 in comp.lang.c++,
allspamgoeshere3@hotmail.com wrote,[color=blue]
>getline(file, line);
>while(!file.eof()){[/color]

Should be:
while(getline(file,line)) {
[color=blue]
>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[/color]

The smarter a debugger is, the less I trust it. I would rather
trust "cerr <<" in the program.
[color=blue]
>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.[/color]

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

Peter Jansson
Guest
 
Posts: n/a
#3: Jan 1 '06

re: Problem with getline and files


allspamgoeshere3@hotmail.com wrote:[color=blue]
> 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
>[/color]

Try this:

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

Regards,
Peter Jansson
Andrew Koenig
Guest
 
Posts: n/a
#4: Jan 2 '06

re: Problem with getline and files


<allspamgoeshere3@hotmail.com> wrote in message
news:1136137229.258878.307320@g43g2000cwa.googlegr oups.com...
[color=blue]
> 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);
> }[/color]

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
}


Marcus Kwok
Guest
 
Posts: n/a
#5: Jan 6 '06

re: Problem with getline and files


Andrew Koenig <ark@acm.org> wrote:[color=blue]
> <allspamgoeshere3@hotmail.com> wrote in message
> news:1136137229.258878.307320@g43g2000cwa.googlegr oups.com...
>[color=green]
>> 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);
>> }[/color]
>
> 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
> }[/color]

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
Roland Pibinger
Guest
 
Posts: n/a
#6: Jan 6 '06

re: Problem with getline and files


On Fri, 6 Jan 2006 18:43:19 +0000 (UTC), ricecake@gehennom.net (Marcus
Kwok) wrote:[color=blue]
>That is nice. I like that better than my usual idiom of
>
> ifstream file("levels\\level1.txt");
> string line;
> while (getline(file, line)) {
> // whatever
> }[/color]

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

Closed Thread