Sabrina Folzeratti
Sabrina Folzeratti

Reputation: 29

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.

Upvotes: 1

Views: 5583

Answers (2)

axon
axon

Reputation: 1200

Beware that in Visual Studio, the Working Directory (set in the project properties under the Debugging tab) is the location that the exe is apparently located when debugging (and not the actual location of the exe itself)

Upvotes: 1

paddy
paddy

Reputation: 63481

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.

Upvotes: 3

Related Questions