reading file question

i notice when i execute the executable and the txt file is in the same folder it does what its supposed to do but when i debug it doesnt open the txt file. Also, im not even sure where to put the txt file for the complier to find the text file so i can debug with it

Also, how can i direct where to find the txt file i want to open?

this is what i have in the main.cpp

      string textLine;

      ifstream ifs("csvTEST.txt", ifstream::in);
      if (ifs.good())   

            while (!ifs.eof()) 
				getline(ifs, textLine);
				cout << textLine << endl;
      } else

            cout << "ERROR: can't open file." << endl;
Last edited on
you need to give relative or absolute path.
Otherwise it searches only in the directory the program is executed in.
eg. if the txt file is in folder Data,
ifstream ifs("Data/csvTEST.txt", ifstream::in);
i dont need to identify which drive its in or anything ?

and is Data within the project directory?
Last edited on
yes, its a relative path. So Data is within projects directory.
if you want to run it from anywhere, give absolute path, like
ifstream ifs("C:/Programfiles/Projectdir/Data/csvTEST.txt", ifstream::in);
@ xkcd83

You can't point to the path like that in C++. You have to use double slashes like this

ifstream ifs("C://Programfiles//Projectdir//Data//csvTEST.txt", ifstream::in);

O_o I don't see why you have to call it relative or absolute your making it sound more confusing than it actually is.

When he wrote "Data/csvTEST.txt" the folder that your .exe is made is will be consider its own directory. So what he wrote told the program to look one level down from the main area into a folder called Data that has a text file called csvTEST.

When you add C://Programfiles//Projectdir//Data//csvTEST.txt its like you are telling the computer to leave your directory and look into another directory also on your computer, also you can tell it to look in your directory as well which is what xkcd83 did.

For example another directory could of been


where the directory is Documents and within that directory 1 level down you would enter folder called textfiles and in that folder there is a file called csvTEST.txt

You should try using the command prompt on your computer it will give you a better idea of how it works.
@Stormhawk: You're getting the slashes mixed up. '\' is an escape character so it needs to be doubled up on. '/' is usually (BUT NOT ALWAYS) an acceptable replacement for what you would normally see in a directory.
Last edited on
@ Computergeek01

Really? I always thought that that wouldn't work since if you use

C:// Documents//wordfiles

then lets say to get out of wordfiles from the directory the game is in for example you would write


where Mytextfile is in up level up from where the game file is in
For example you got the game in C://Games//WoW//WoW.exe
So you would of went up to the Games folder.

So the other \ is to go up a level in the directory while / is to go down a a level.
Just think of the code you use to make a newline ('\n') or a tab indentation('\t'). You type it like this because the backslash is an escape character.
ok I see what you mean, and I did mix up the two xD.
Topic archived. No new replies allowed.