Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
959 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...