Why my program can't find the file when opening .txt files?

182 Views Asked by At

I'm currently learning file I/O in C++.
I can open the file by using the full directory of the .txt file which is C:\Users\mfkha\cmsc202\inclass\scores.txt.
The path of the program file is C:\Users\mfkha\cmsc202\inclass\Lesson 4.cpp.
However, when I am using scores.txt, the program can't find the file.

Here is the source code:

#include <cstdlib>
#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
using namespace std;
int main() {
    string fName;
    string lName;
    int score;

    // ifstream myFile("testfile.txt"); // OPTION 1
    ifstream myFile;
    myFile.open("C:\\Users\\mfkha\\cmsc202\\inclass\\scores.txt"); // OPTION 2
    if (myFile.is_open()) {
        while(myFile >> fName && myFile >> lName && myFile >> score) {
            cout << fName << " " << lName << " " << score << endl;
        }
        myFile.close();
    }
    else{
        cout << "No file found" << endl;
    }
    return 0;
}

I can't open the file when I was using file name, I have to use full path tof the file, someone please help.

3

There are 3 best solutions below

2
C. Wells On

Every process has a working directory, initially set to the directory from which the executable was launched.

For instance, if the executable g++ is run from "C:\users\bob\Desktop", the process will start with its working directory set to "C:\users\bob\Desktop".

File paths must be specified relative to this working directory, or as absolute paths from the filesystem's root directory.

For example, if Lesson4.cpp is compiled into an executable named lesson4 in "C:\users\bob\Desktop", accessing "testfile.txt" requires the file be in the same directory as from where the executable was launched.

It is possible to change the working directory of your process using the system call "chdir()".

To access "testfile.txt" in your case, either use chdir() to switch to its directory or execute the resulting binary from your C++ code from the same directory as "testfile.txt".

1
Charley On

As @drescherjm commented,

There is a concept called current working directory which is the folder that your program will look in. That folder may be the same as your executable or possibly the same as your project directory.

Well, I have tried your code, and I am using VSCode too. But your code works fine on my machine, therefore I think that your CWD (Current Working Directory) is different from the directory of the program file. You can use cd to check the CWD. If the CWD is really different, you can probably set it by configuring the launch.json, see here, as @john commented.
Another way to change the CWD is using the chdir function, as commented in the comment section. You should include the unistd.h or the io.h standard C library first. For example, you can use chdir("C:\\Users\\mfkha\\cmsc202\\inclass"); in your program.

4
Dúthomhas On

There are some very easy basic assumptions that can be made here:

  • OP is using Windows
  • OP is using MSVC (Visual Studio)
  • OP is using an IDE (probably MSVC’s, but even if it isn’t)

Default project options in MSVC are to put your executable file in sub-folders of your program’s source code folder. If your project is in

C:\Users\Muhammad\Documents\Projects\my-program\

Then your executable will be in something like:

C:\Users\Muhammad\Documents\Projects\my-program\bin\Release\win32\my-program.exe or C:\Users\Muhammad\Documents\Projects\my-program\bin\Debug\win32\my-program.exe

You can change your Project settings in the IDE to put the .exe in the same folder as the source files. https://learn.microsoft.com/en-us/visualstudio/ide/how-to-change-the-build-output-directory?view=vs-2022

Another simple option is to just compile your program, then copy the input file to the same folder as the .exe file. After that you can run the program from the IDE and it will work.

From the Terminal

I personally find that it is worth your time to learn how to frob the compiler from the Windows Terminal. It is really easy once you get past the “how to use the terminal” bit.

If you start the Visual Studio Developer Command Prompt you can use the following commands:

  • cd C:\Users\Muhammad\Documents\Projects\my-program to change to your source code directory

  • dir to list all the files in the folder (which should include my-program.exe and scores.txt)

  • cl /EHsc /W4 /Ox /std=c++17 my-program.cpp to compile your program using C++17 with all warnings

  • my-program to run your program (assuming it compiled successfully)

I think I am in the minority about preferring the terminal over the IDE, though...

It just takes a bit more learning to get used to your development environment (that is, to become familiar with your tools and how to use them). Keep going! You’ll get there just fine!