Connecting Tech Pros Worldwide Help | Site Map

Problem with getline and files

 
LinkBack Thread Tools Search this Thread
  #1  
Old January 1st, 2006, 04:55 PM
allspamgoeshere3@hotmail.com
Guest
 
Posts: n/a
Default Problem with getline and files

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, 05:15 PM
David Harmon
Guest
 
Posts: n/a
Default 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, 05:15 PM
Peter Jansson
Guest
 
Posts: n/a
Default 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, 05:05 PM
Andrew Koenig
Guest
 
Posts: n/a
Default 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, 05:55 PM
Marcus Kwok
Guest
 
Posts: n/a
Default 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, 10:05 PM
Roland Pibinger
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.