Thread: ofstream and the default path

  1. #1
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120

    ofstream and the default path

    Hi there I am using an ofstream object to write to files and I want to know if there is a method in which I can change the default path from the app location.

    i.e.

    My app is at C:\app\app.exe and I want to open the file C:\folder\file.txt by using:

    Code:
    ofstream file("file.txt");
    Any help would be much appreciated

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What's wrong with building the path from a default directory and a filename?
    Code:
    string default_path("C:\folder\");
    string default_file("file.txt");
    
    ...
    
    string filename(default_path + default_file);
    ofstream file(filename.c_str());
    Then you can have whatever defaults you want and still allow users to change their preference.
    My best code is written with the delete key.

  3. #3
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    Well indeed nothing. But I had problems using my CString object that contained the path string. I've just this minute discovered I needed to use ReleaseBuffer() on it. Doh

    tx for you help tho

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Doh
    That'll teach you to not use MFC objects in the first place.
    My best code is written with the delete key.

  5. #5
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    lol, needed a "quick and easy" solution for a tiny program

    While your here, I've already spent long enuff finding the answer to this one;

    How can I check to see whether a directory exists or not?

    thnx in advance (this will be the final question to see the completion of this "quick n easy" program)

    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Try to stat() it, or any other file information function. If the call fails, the directory probably doesn't exist; check the error value to be sure.
    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

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How can I check to see whether a directory exists or not?
    Quick and dirty solution:
    Code:
    #include <windows.h>
    
    bool exists(const char *path)
    {   
      return GetFileAttributes(path) == FILE_ATTRIBUTE_DIRECTORY;   
    }
    My best code is written with the delete key.

  8. #8
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    many thanks again, after not being able to find f' all on stat() I eventually stumbled on _access(), which you can use on a file or directory. It's in io.h and works v nicely indeed.


    dt
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by Prelude
    >How can I check to see whether a directory exists or not?
    Quick and dirty solution:
    Code:
    #include <windows.h>
    
    bool exists(const char *path)
    {   
      return GetFileAttributes(path) == FILE_ATTRIBUTE_DIRECTORY;   
    }
    Code:
    #include <windows.h>
    
    bool is_folder(const char *path)
    {   
      DWORD dwAttrs = GetFileAttributes(path);
    
      return (dwAttrs != INVALID_FILE_ATTRIBUTES && (dwAttrs & FILE_ATTRIBUTE_DIRECTORY));
    }

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or you can use Boost.Path. It provides all the functionality you need and more.
    http://www.boost.org/
    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