1. ## ifstream question

I have a bit of code that is SUPPOSED to open a file and read a single line from it. That's all fine and dandy. Now, i'd like for the path to be relative to the compiled .exe, which is also fine and dandy, and how I thought the ifstream.open("text.txt"); was supposed to work.

However, if I don't hardcode the path, it refuses to open. IE, I can't put "text.txt" and the program be happy. I have to put C:\\Whatever\\text.txt. Is this how it's supposed to work? Because, that seems weird.

I'm using Bloodshed Dev-C++ 4 with Mingw, btw.

2. Put the input file and the executable in the same directory and run from the command line?

3. Originally Posted by Dave_Sinkula
Put the input file and the executable in the same directory and run from the command line?
Yeh, that doesn't work. That's what i'm saying, they're in the same directory, however using a relative path to the file doesn't work.

4. If they are both in the same directory, what does a relative path have to do with this?

5. An absolute path is c:\whatever\whatever\text.txt, a relative path isn't. Having the files in the same directory and using ifstream.open("text.txt") is a relative path, instead of an absolute path to the file.

They're in the same directory, but unless I put the full path to the file, it won't read it.

6. Does the program create the file that's being read?? If not, that may be the problem....since some compilers create a temporary .exe somewhere else to run after compilation... Could be the cause why it doesn't find the file where the temporary .exe is being runned....

7. Originally Posted by Dekon
An absolute path is c:\whatever\whatever\text.txt, a relative path isn't. Having the files in the same directory and using ifstream.open("text.txt") is a relative path, instead of an absolute path to the file.

They're in the same directory, but unless I put the full path to the file, it won't read it.
Are you running the executable from this directory, or is an IDE doing some magic?

8. Originally Posted by Dave_Sinkula
Are you running the executable from this directory, or is an IDE doing some magic?
I've tried both. I have no idea why this doesn't work properly. It's angering.

9. *Ahem*

Are you running the executable from the command line?

Are you running the executable from an IDE?

How are you getting either "c:\whatever\whatever\text.txt" or "C:\\Whatever\\text.txt"?

If the path contains spaces, are you enclosing it in double quotes?

Shall we continue playing 20 questions?

10. Originally Posted by Dave_Sinkula
*Ahem*

Are you running the executable from the command line?

Are you running the executable from an IDE?

How are you getting either "c:\whatever\whatever\text.txt" or "C:\\Whatever\\text.txt"?

If the path contains spaces, are you enclosing it in double quotes?

Shall we continue playing 20 questions?
Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

string x;

//ifstream wordfile("c:\\documents and settings\\owner\\my documents\\code\\word.txt");
ifstream wordfile("word.txt");
getline(wordfile, x);

cout << x << endl;

cin.get();

}
The commented out part is the path that works. The second path, does not. And yes, i've tried BOTH ways of running the executable(ide and cmd). My lack of c++ knowledge doesn't translate to a complete lack of any computer knowledge. I've just mainly been hardware up to this point.

11. How are you running this from the command line with the file and the executable in the same directory?
c:\documents and settings\owner\my documents\code>myapp.exe
Have you considered using perror to tell you why the file does not open when it fails after you *hint* check whether the stream is valid?

12. Originally Posted by Dave_Sinkula
How are you running this from the command line with the file and the executable in the same directory?

Have you considered using perror to tell you why the file does not open when it fails after you *hint* check whether the stream is valid?
Yes, as i've stated twice now. I've tried both ways of running it. The CLI/CMD/Prompt/Shell and via the IDE's magic clicky button to execute.

I have no clue what perror is. Lemme google it and get back to you.

Thanks for the help.

13. Originally Posted by Dekon
Yes, as i've stated twice now. I've tried both ways of running it. The CLI/CMD/Prompt/Shell and via the IDE's magic clicky button to execute.

I have no clue what perror is. Lemme google it and get back to you.

Thanks for the help.
The IDE may not be running it from where you think, that's why I mentioned that first.

perror is a function in C. Used something like this:
Code:
ifstream wordfile("word.txt");
if ( !wordfile )
{
perror("word.txt");
}

14. Originally Posted by Dave_Sinkula
The IDE may not be running it from where you think, that's why I mentioned that first.

perror is a function in C. Used something like this:
Code:
ifstream wordfile("word.txt");
if ( !wordfile )
{
perror("word.txt");
}
Started a new project, and the current(er, the new current. not the original.) one (using the -same- code, mind you) runs fine from the command line, but not from the magic IDE clicky button.

Care to elaborate possibly on why the IDE would run it from somewhere other than it's saved to directory?

perror looks handy, i'll have to learn more about that (and error checking in general, ugh, so much to learn).

15. Originally Posted by Dekon
Care to elaborate possibly on why the IDE would run it from somewhere other than it's saved to directory?
SOP, often. Perhpas something like this:

You have your source in one directory. It builds the relocatable object files into a subdirectory (often), and places the resulting executable there as well. When it runs the executable, it runs it from where the "project directory" is (maybe not where the executable was put, and which may be different from where the source code is).

Happens all the time.