ifstream open file, but wrong directory

This is a discussion on ifstream open file, but wrong directory within the C++ Programming forums, part of the General Programming Boards category; I have previously used a windows open file dialog box, which I am guessing changes the directory to whatever I ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    23

    ifstream open file, but wrong directory

    I have previously used a windows open file dialog box, which I am guessing changes the directory to whatever I selected there.
    So when I call ifstream f1("file.txt") it only looks for the file in the directory I chose with the dialog box. How do I set the directory back to the one where the program is running from?
    I guessed this was a general c++ question not a windows one, despite the dialog box. Correct me if I am wrong, but I desperately need an answer!

    thanks a lot,
    esaptonor

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    This is generally called the current directory (or maybe working directory), and I believe you do need a Windows solution for it. Perhaps SetCurrentDirectory?

    Note that even without your dialog, the current directory is not necessarily the directory the exe is in, so you have to be careful about doing things like f1("file.txt").

    If you want to set the current directory to whatever the current directory was at the beginning of the program, you'll want to se GetCurrentDirectory (or whatever it is) when your program starts and save the result for later. If you want to set the current directory to the directory the exe is in, you'll need to use more windows functions to figure that out, and of course you should do that regardless of whether you use a open file dialog.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21