Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to load and save I/O files from/to different directory?

I guess I lost my post I just wrote here.
I am looking for the help for you.

In C++ program, I use ifstream and ofstream to load files and save files but they happen to be in the current path folder that the program locates. But anybody know how to do so with input files and output files that locate in other path folders. In windows, the path relations use ..\..\, but in Unix they use ../../, how to manage this difference suppose to run the program in Unix?

Thanks in advance.

AW
[515 byte] By [alvinw] at [2007-11-11 6:34:21]
# 1 Re: How to load and save I/O files from/to different directory?
If the program is aimed at Linux/Unix then you simply have to use the /../ notation for directories. If you want the program to run on both Windows and Unix, you can define the directory name as an environment variable or a string object and append it to the file name:

string filename fn("mydata.dat");
string path;
if (UNIX)
path = "/mydir/mydata";
if (Windows)
path = "\\mydir\\mydata"; //notice: double slashes required

filename += path + filename; //concatenate the two
Danny at 2007-11-11 21:03:00 >
# 2 Re: How to load and save I/O files from/to different directory?
Thank you very much, Danny.
I havn't gone them to try in my code. I have more questions on this.

Are 'UNIX' and 'Windows' the key words in the C++ source code? If not, how do I define them as environment variables that can be automatically recognized and be used within my C++ surce code?

About the path, suppose my source code "prog.cpp" locates at ..\My Project\project01\src\prog.cpp, and my data file "mydata.dat" locates at ..\My Project\project01\result\mydata.dat, is it right to locate my data file as: "..\\result\\mydata.dat" in my C++ source code?
For example, can I use this line to open it? - ifstream in("..\\result\\mydata.dat");
About path folder name, will My Project (with a white space in between) be treated as a valide folder name?
Thank you very much.

AW
alvinw at 2007-11-11 21:04:11 >
# 3 Re: How to load and save I/O files from/to different directory?
you can use them without defining.

for Unix
#if defined (_UNIX)

for Wins
#if defined (_WINDOWS)
Butterfly at 2007-11-11 21:05:10 >
# 4 Re: How to load and save I/O files from/to different directory?
for Unix
#if defined (_UNIX)

for Wins
#if defined (_WINDOWS)
Is there a list of these _ALLCAPITIALS type environment constants that are either on most computers or guarenteed to be on all computers you'll be running the code on? Is this a computer defined or a compiler defined construct?
bpw1621 at 2007-11-11 21:06:04 >
# 5 Re: How to load and save I/O files from/to different directory?
There's no such list since each compiler, OS and user define them on their own. However, you can use the getenv() standard function and the envp environment variable to examine them. getenv() returns the translation of a single environment variable, whereas envp is a list of all the environment variables defined for a process. See:
http://www.dev-archive.com/dev-archive/LegacyLink/9481
BTW, don't confuse environment variable with macro symbols. Normally _UNIX and _WINDOWs are defined as such. To view them, check the header files that your app #includes, particularly the ones bearing the platform-specific declarations such as "windows.h", "stdafx.h", "unist.h" and so on.
Danny at 2007-11-11 21:07:08 >