Thread: ifstream not working

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    9

    ifstream not working

    Hi guys,
    for some reason my file doesn't get open its in same directory as the executable.
    Here is my code
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    
    using namespace std;
    int main(void)
    {
        ifstream    in;
        ofstream    out;
        string        data;
    
    
        in.open("x.txt");
        if(!in.good()){
            cout << "Error opening the input file for reading data\n";
            return 1;
        }
        out.open("output.txt");
        if(!out.good()){
            cout << "Error opening the output file for writing data\n";
            return 1;
        }
        getline(in,data);
        cout << data << endl;
        return 0;
    
    
        
        return 0;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What IDE and OS are you using?

    If it's MSVC2010 (on windows) then you don't want to put the data file in the same directory as the executable. Instead, put it here (assuming a project called testfile) :
    Code:
    Visual Studio 2010
        Projects
            testfile
                testfile
                    put data files here
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    I am using windows 7 with MSVC2008

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Presumably that's not much different from MSVC2010. Did you try the above? Or something similar? The basic idea is that the data file(s) may need to go somewhere other than where the executable is.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    yeh it works now thanks alot

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    No problem, my friendly. Keep on codin'!
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    9
    thanks buddy I sure will .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  2. ifstream help
    By JustMe in forum C++ Programming
    Replies: 8
    Last Post: 06-03-2007, 02:34 PM
  3. ifstream
    By krappykoder in forum C++ Programming
    Replies: 7
    Last Post: 12-20-2002, 11:38 AM
  4. little help on ifstream
    By ssjnamek in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 12:10 PM