Thread: Newbie Question: File Input and Relative Paths

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    37

    Question Newbie Question: File Input and Relative Paths

    Hello,

    First of all, I apologize if this question has been answered before, but I browsed through hundreds of posts, and couldn't find anything thus far.

    I have a program in a specified path, d:\ashiq\c++\game. All of my .cpp files are in there, as well as some plain text files I wish to input during the course of my program.

    However, I find that there is no way to open a file (for input) without putting in SOME sort of path. By default, it will check my compiler path, which is NOT what I want--I want it to use the program path. Is there a way to do this, other then manually putting in the path? Here's my IO code.

    Code:
    int showIntro()
    {
     ifstream logoStream("intro.txt");
    
     if (!logoStream)
     {
     cout << "File did not open!";
     } else {
    
      string line= "";
    
      getline(logoStream, line);
    
      while (line != "")
      {
       cout << line << "\n";
       getline(logoStream, line);
      }
     }
    
     logoStream.close();
    
     return 0;
    }
    Is it possible to get the program .exe file path, and to use relative paths? as in /data/art/temp.jpg as opposed to putting the entire path?

    Cheers,

    --Ashiq

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    37
    Oops, sorry, nearly forgot:

    I'm running Dev-C++ 4.x on Windows 2000, and this is for a console application.

    --Ashiq

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is it possible to get the program .exe file path, and to use relative paths?
    As I understand your question, no. Such functionality would only come from a compiler option, and I've not been able to find it after a cursory search of your compiler's manual. Of course, it shouldn't be difficult to set up a path in a header file somewhere and concatenate the file name onto it when you need to open a file in a directory other than where the executable resides.
    My best code is written with the delete key.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If you want the path of the compiled EXE file, just use argv[ 0 ].
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If you want the path of the compiled EXE file, just use argv[ 0 ].
    This isn't guaranteed. argv[0] could be the full path, just the file name, a partial path, or even an empty string.
    My best code is written with the delete key.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I thought it was always the full path to the EXE. Oh well.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I thought it was always the full path to the EXE.
    That would be nice. I say we submit a defect report about it.
    My best code is written with the delete key.

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If you're using Windows, you could try GetModuleFileName( ).

    Code:
    #include <iostream>
    #include <cstring>
    #include <windows.h>
    
    #define maxval( a, b ) ( (a) > (b) ? (a) : (b) )
    
    int main( void ) {
        
        char fileName[ MAX_PATH ];
    
        int pathLength = GetModuleFileName( GetModuleHandle( NULL ), fileName, MAX_PATH );
    
        if( !pathLength ) std::cout<<"Couldn't find path."<<std::endl;
    
        else {
    
            char *lastSlash = maxval( strrchr( fileName, '\\\' ), strrchr( fileName, '/' ) ) + 1;
    
            *lastSlash = '\0';
    
            std::cout<<"File path: "<<fileName<<std::endl;
    
        }
    
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    37
    Well...geez. What if I want to give my program to someone? ...How do other companies get around this? For surely, most programs, if not all, allow you to specify an installation path...

    Well thanks, I noticed the argv[0] thing...mine gives a full path, but I guess that's not very reliable. I'll try that anyway.

    --Ashiq

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    You can use the following API function. You must get it at the beginning of your program as the current directory may change during execution.

    DWORD GetCurrentDirectory(
    DWORD nBufferLength, // size, in characters, of directory buffer
    LPTSTR lpBuffer // pointer to buffer for current directory
    );


    you must use include <winbase.h>

    example
    Code:
    char buffer[255];
    GetCurrentDirectory(255, buffer);
    zMan

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    GetCurrentDirectory won't necessarily return the directory that the EXE is in, though.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There should be an option somewhere in the Dev-C++ options (maybe project options) that allows you to set the initial working directory for your app. Set it to your exe's path and the relative filenames will work.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. relative paths with XPath in libxml2
    By sparcz in forum C Programming
    Replies: 3
    Last Post: 10-12-2006, 02:49 AM
  2. Replies: 9
    Last Post: 03-17-2006, 12:44 PM
  3. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM