Connecting Tech Pros Worldwide Help | Site Map

Problem with getline and files

  #1  
Old January 1st, 2006, 05:55 PM
allspamgoeshere3@hotmail.com
Guest
 
Posts: 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

  #2  
Old January 1st, 2006, 06:15 PM
David Harmon
Guest
 
Posts: n/a

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.

  #3  
Old January 1st, 2006, 06:15 PM
Peter Jansson
Guest
 
Posts: n/a

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
  #4  
Old January 2nd, 2006, 06:05 PM
Andrew Koenig
Guest
 
Posts: n/a

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
}


  #5  
Old January 6th, 2006, 06:55 PM
Marcus Kwok
Guest
 
Posts: n/a

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
  #6  
Old January 6th, 2006, 11:05 PM
Roland Pibinger
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading binary files: problem with argument types freeurmind answers 1 October 27th, 2007 05:25 PM
problem with getline ankit.kumar.agarwal@gmail.com answers 6 March 11th, 2007 08:05 PM
Problem with C++ Compiling Error ld: Unresolved Error stevenruiz@gmail.com answers 2 February 13th, 2007 12:15 AM
Problem with a diff algorithm Eric Boutin answers 4 July 22nd, 2005 11:34 PM