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


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

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

  1. #1
    Join Date
    Aug 2005
    Posts
    14

    Unhappy 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

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    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 Kalev

  3. #3
    Join Date
    Aug 2005
    Posts
    14
    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

  4. #4
    Join Date
    May 2005
    Posts
    53
    you can use them without defining.
    for Unix
    #if defined (_UNIX)

    for Wins
    #if defined (_WINDOWS)

  5. #5
    Join Date
    Jul 2005
    Posts
    22
    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?

  6. #6
    Join Date
    Nov 2003
    Posts
    4,118
    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.devx.com/DevX/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 Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center