Page 1 of 1

ifstream reading

Posted: Mon Dec 13, 2010 11:27 am
by like80ninjas
I'm trying to read in a file line by line and just print it to the screen. I know how to do this, and have done it many times, but for some reason my code isn't printing!

string index;
ifstream mapfile("map.txt");
while (mapfile)
{
getline(mapfile,index);
cout << index;
}
mapfile.close();

I've included <iostream> and <fstream> properly, and it all compiles no problem. Also, the map.txt IS in the right directory and it's all setup etc, it simply WILL NOT print to console and I can't figure out why.
Any help guys?

Re: ifstream reading

Posted: Mon Dec 13, 2010 12:21 pm
by dandymcgee
Try while(!mapfile.eof()). I suspect an infinite loop.

Re: ifstream reading

Posted: Mon Dec 13, 2010 12:27 pm
by like80ninjas
Yeah, that causes an infinite loop, what's the problem?

Re: ifstream reading

Posted: Mon Dec 13, 2010 12:41 pm
by MadPumpkin
like80ninjas wrote:I'm trying to read in a file line by line and just print it to the screen. I know how to do this, and have done it many times, but for some reason my code isn't printing!

string index;
ifstream mapfile("map.txt");
while (mapfile)
{
getline(mapfile,index);
cout << index;
}
mapfile.close();

I've included <iostream> and <fstream> properly, and it all compiles no problem. Also, the map.txt IS in the right directory and it's all setup etc, it simply WILL NOT print to console and I can't figure out why.
Any help guys?
Alright so first, you should keep all of your posted codes in code tags so that it's more organized.

So here is what it should look like, just a few minor changes. I haven't compiled this but it really should work fine.

Code: Select all

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

int main () {
   string index;
   ifstream mapfile("map.txt");
   if(mapfile.is_open()) //This is what actually checks if it's open I recommend not just "if(mapfile){ etc. }"
   {
      while(!mapfile.eof()) //While not the end of file
      {
         getline(mapfile,index);
         cout << index << endl;
      }
      mapfile.close();
   }
   return 0;
}

Re: ifstream reading

Posted: Mon Dec 13, 2010 12:49 pm
by like80ninjas
First, I retyped your suggested code, same result, then I copy pasted it, same result. It simply won't print anything. The only thing I can think of is that the text file is in the wrong directory but it isn't. It's in the project directory, where the source is located, and it's being run from the ide(visual studio) all my images load fine from this directory.

Re: ifstream reading

Posted: Mon Dec 13, 2010 12:51 pm
by dandymcgee
like80ninjas wrote:Yeah, that causes an infinite loop, what's the problem?
The standard out stream is probably not being flushed until that loop ends, which it never does.

Re: ifstream reading

Posted: Mon Dec 13, 2010 12:52 pm
by MadPumpkin
like80ninjas wrote:First, I retyped your suggested code, same result, then I copy pasted it, same result. It simply won't print anything. The only thing I can think of is that the text file is in the wrong directory but it isn't. It's in the project directory, where the source is located, and it's being run from the ide(visual studio) all my images load fine from this directory.
It has to be where the application itself is located, so where ever you are running the application from, has to have the "map.txt" in it.
EDIT: unless you put something like "Maps/Forest Maps/map.txt" instead of just "map.txt" as the file name.

Re: ifstream reading

Posted: Mon Dec 13, 2010 1:02 pm
by like80ninjas
I'm running the game from my compiler, visual studio, and it's directory is the same directory you keep your source files. All of my images, and DLLs are located there as well and load fine.

Re: ifstream reading

Posted: Mon Dec 13, 2010 1:18 pm
by dandymcgee
Try making a new project in a new folder.

Re: ifstream reading

Posted: Mon Dec 13, 2010 1:32 pm
by like80ninjas
Why would that help my cause? I would prefer not to make a new project.

Re: ifstream reading

Posted: Mon Dec 13, 2010 3:03 pm
by TheBuzzSaw

Code: Select all

string s;
while (getline(mapfile, s))
{
    // do stuff
}
That's the best way to do it. ^^^

Re: ifstream reading

Posted: Mon Dec 13, 2010 3:04 pm
by dandymcgee
Try changing #include<string.h> to #include<string>. That is the only thing I had to change to compile MadPumpkin's code and have it work correctly.

Re: ifstream reading

Posted: Mon Dec 13, 2010 3:22 pm
by like80ninjas
In my code I do use <string> rather than <string.h>
But, i'll try TheBuzzSaw's method and see how it turns out.
I'm just trying to get a system built to load a map from text, and I've done it before in multiple engines, but this time it just won't work!

Re: ifstream reading

Posted: Mon Dec 13, 2010 9:41 pm
by TheBuzzSaw
Yeah you have to be careful with the string libraries. The C library is string.h or cstring when including from C++. The STL string library is just <string>.