views:

83

answers:

4

I moved from Windows to Mac and now I'm experiencing a problem with the file input/output classes: ifstream & ofstream.

In Windows when you run with g++/Code Blocks

ofstream out("output.txt");
out << "TEST";
out.close();

A new file "output.txt" will be created in the same directory.

However in MAC OS X, this file is created in my home directory: /Users/USER_NAME/output.txt

How can I have this file in the same directory together with the executable?

P.S. I'm using GCC and CodeBlocks. There are no projects - I'm just compiling a single source file.

UPDATE

I just ran the same code in XCode, and it works ok! The files are created in project's Debug directory.

So it's not operating system's problem. It's Code::Blocks for Mac. How can I change work directory in Code::Blocks?

A: 

You'll need to provide a full, absolute path to the file you are trying to create.

Noah Roberts
+4  A: 

The stream classes, like all other file-opening functions, use the current directory when you provide a relative path. You can control the current directory with a function like chdir, but a better solution is to use fully qualified file names. Then you remove your program's dependency on the current directory.

Rob Kennedy
I'm doing little C++ problems. I have dozens of those. And if I specify an absolute path - the programs won't run on Windows. Besides, if I change the directory, they won't work either.
Alex
@Alex Absolute doesn't mean constant :-D You of course need to generate the absolute path from the current program position. Use argv[0] to see the relative path used to execute the program.
Let_Me_Be
@Let_Me_Be, but Rob said that "a better solution is to use fully qualified file names"... I'm totally confused. I just need to access files from the directory where the executable is.
Alex
@Alex Well, I'm saying the same thing. The point is that you need to construct the path for each run of the program. You can't just add some constant into your code.
Let_Me_Be
I didn't say you needed a *hard-coded* absolute path. Often, the location of the executable is given by `argv[0]`. It's not guaranteed, but it's probably reliable enough for your purposes — remove the executable name, append your desired file name, and you'll have a dynamically generated absolute path suitable not only for passing to `ofstream`, but also suitable for printing to the debugger if `ofstream` doesn't open the file you expected. Debugging "file not found" problems is *a lot* easier when you know exactly what file your program was trying to find in the first place.
Rob Kennedy
Yes, I just checked, argv[0] gives a full location of the executable. So I have to remove the executable filename, then call chdir() with what I got, and I'll be able to use in.open("input.txt"), right? I'll try it right now.
Alex
@Alex: Watch out, I think at least under some circumstances `argv[0]` reflects the command as given. That is, if you do not provide a path when invoking the command, it might just the executable's name, if you provide a relative path + executable, it might be that. I'm not sure which OS follows which convention (and under which circumstances), though.
sbi
I concur with sbi.
Steve M
Don't rely on the current directory to remain the same forever, @Alex, especially on Windows. Use what I described instead to generate fully qualified names for everything you open. See also, [Finding current executable's path without /proc/self/exe](http://stackoverflow.com/questions/1023306).
Rob Kennedy
I just ran the same code in XCode, and it works ok! The files are created in project's Debug directory. So it's not operating system's problem. It's Code::Blocks for Mac. How can I change work directory in Code::Blocks?
Alex
@Alex, you're missing the point. Stop relying on the current directory. But, if you really must change the Code Blocks directory, then ask that as a *new question*. You've already gotten your answer about where the stream classes put their files. Changing the nature of a question after you've gotten answers is frowned upon and tends to garner negative votes. Post a new question instead.
Rob Kennedy
+2  A: 

The file is simply created in the current working directory. Change working directory or provide full path.

Let_Me_Be
How can I change working directory?
Alex
You will need to use the OS API for that. In POSIX it is chdir: http://www.opengroup.org/onlinepubs/009695399/functions/chdir.html
Let_Me_Be
+1  A: 

The working directory is initially set when your program starts. When you start it from the command line, you inherit the current working directory from the shell. In CodeBlock, one of the project options is the execution working dir' for debug runs.

(See also http://www.gamedev.net/community/forums/topic.asp?topic_id=571206&amp;whichpage=1&#3648738)

MSalters
Even when I start it from the command line, it still creates the text files in USER folder.
Alex