Thread: ifstream question

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    ifstream question

    I have a bit of code that is SUPPOSED to open a file and read a single line from it. That's all fine and dandy. Now, i'd like for the path to be relative to the compiled .exe, which is also fine and dandy, and how I thought the ifstream.open("text.txt"); was supposed to work.

    However, if I don't hardcode the path, it refuses to open. IE, I can't put "text.txt" and the program be happy. I have to put C:\\Whatever\\text.txt. Is this how it's supposed to work? Because, that seems weird.

    I'm using Bloodshed Dev-C++ 4 with Mingw, btw.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Put the input file and the executable in the same directory and run from the command line?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Quote Originally Posted by Dave_Sinkula
    Put the input file and the executable in the same directory and run from the command line?
    Yeh, that doesn't work. That's what i'm saying, they're in the same directory, however using a relative path to the file doesn't work.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If they are both in the same directory, what does a relative path have to do with this?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    An absolute path is c:\whatever\whatever\text.txt, a relative path isn't. Having the files in the same directory and using ifstream.open("text.txt") is a relative path, instead of an absolute path to the file.

    They're in the same directory, but unless I put the full path to the file, it won't read it.

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Does the program create the file that's being read?? If not, that may be the problem....since some compilers create a temporary .exe somewhere else to run after compilation... Could be the cause why it doesn't find the file where the temporary .exe is being runned....
    Be easy on me...only 14

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Dekon
    An absolute path is c:\whatever\whatever\text.txt, a relative path isn't. Having the files in the same directory and using ifstream.open("text.txt") is a relative path, instead of an absolute path to the file.

    They're in the same directory, but unless I put the full path to the file, it won't read it.
    Are you running the executable from this directory, or is an IDE doing some magic?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Quote Originally Posted by Dave_Sinkula
    Are you running the executable from this directory, or is an IDE doing some magic?
    I've tried both. I have no idea why this doesn't work properly. It's angering.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    *Ahem*

    Are you running the executable from the command line?

    Are you running the executable from an IDE?

    How are you getting either "c:\whatever\whatever\text.txt" or "C:\\Whatever\\text.txt"?

    If the path contains spaces, are you enclosing it in double quotes?

    Shall we continue playing 20 questions?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Quote Originally Posted by Dave_Sinkula
    *Ahem*

    Are you running the executable from the command line?

    Are you running the executable from an IDE?

    How are you getting either "c:\whatever\whatever\text.txt" or "C:\\Whatever\\text.txt"?

    If the path contains spaces, are you enclosing it in double quotes?

    Shall we continue playing 20 questions?
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
    string x;
    
    //ifstream wordfile("c:\\documents and settings\\owner\\my documents\\code\\word.txt");
    ifstream wordfile("word.txt");
    getline(wordfile, x);
    
    cout << x << endl;
    
    cin.get();
    
    }
    The commented out part is the path that works. The second path, does not. And yes, i've tried BOTH ways of running the executable(ide and cmd). My lack of c++ knowledge doesn't translate to a complete lack of any computer knowledge. I've just mainly been hardware up to this point.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How are you running this from the command line with the file and the executable in the same directory?
    c:\documents and settings\owner\my documents\code>myapp.exe
    Have you considered using perror to tell you why the file does not open when it fails after you *hint* check whether the stream is valid?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Quote Originally Posted by Dave_Sinkula
    How are you running this from the command line with the file and the executable in the same directory?

    Have you considered using perror to tell you why the file does not open when it fails after you *hint* check whether the stream is valid?
    Yes, as i've stated twice now. I've tried both ways of running it. The CLI/CMD/Prompt/Shell and via the IDE's magic clicky button to execute.

    I have no clue what perror is. Lemme google it and get back to you.

    Thanks for the help.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Dekon
    Yes, as i've stated twice now. I've tried both ways of running it. The CLI/CMD/Prompt/Shell and via the IDE's magic clicky button to execute.

    I have no clue what perror is. Lemme google it and get back to you.

    Thanks for the help.
    The IDE may not be running it from where you think, that's why I mentioned that first.

    perror is a function in C. Used something like this:
    Code:
    ifstream wordfile("word.txt");
    if ( !wordfile )
    {
       perror("word.txt");
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Quote Originally Posted by Dave_Sinkula
    The IDE may not be running it from where you think, that's why I mentioned that first.

    perror is a function in C. Used something like this:
    Code:
    ifstream wordfile("word.txt");
    if ( !wordfile )
    {
       perror("word.txt");
    }
    Started a new project, and the current(er, the new current. not the original.) one (using the -same- code, mind you) runs fine from the command line, but not from the magic IDE clicky button.

    Care to elaborate possibly on why the IDE would run it from somewhere other than it's saved to directory?

    perror looks handy, i'll have to learn more about that (and error checking in general, ugh, so much to learn).

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Dekon
    Care to elaborate possibly on why the IDE would run it from somewhere other than it's saved to directory?
    SOP, often. Perhpas something like this:

    You have your source in one directory. It builds the relocatable object files into a subdirectory (often), and places the resulting executable there as well. When it runs the executable, it runs it from where the "project directory" is (maybe not where the executable was put, and which may be different from where the source code is).

    Happens all the time.

    Drives you mad sometimes.


    The short version is to run the code from an actual command shell.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Simple ifstream Question
    By Paul22000 in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2008, 05:34 PM
  3. question about ifstreams
    By Amyaayaa in forum C++ Programming
    Replies: 6
    Last Post: 05-27-2008, 03:48 PM
  4. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM