Search code examples
c++visual-studio-2010ifstream

ifstream can't find file that is saved in the directory


I'm writing a program that uses <fstream>. The file I need to include is called employee.dat and it's listed in the directory. I put in a cout statement that says:

ifstream inFile ("EMPLOYEE.DAT");

if (! inFile)
{
    cout << "!!Error in opening 'EMPLOYEE.DAT"<< endl;
}

The file is in the directory and there shouldn't be any issues opening/finding it. This is my first time using the ifstream class.


Solution

  • If you are running from the Visual Studio environment, be aware that your executable probably isn't in the same directory as your data file. Binaries tend to be built into a Debug or Release folder by default. You have several options:

    1. Move your data file into the correct directory.
    2. Set the Working Directory (in your project settings, under "debugging").
    3. Use a full or relative path for your file name.

    It's also possible that you are looking in the right place, but the file is locked. Make sure it's not open in any editor or other program that might prevent other processes from opening it.