I'm writing a program for my laboratory work using C++. But I have a strange problem that I see first time. I can't open my text file using the ifstream::open
method.
This simple code doesn't work too:
int main() {
ifstream file;
file.open("numbers.txt");
if (file.is_open())
cout << "fstream.open(c:\\numbers.txt): it's ok." << endl;
else
cout << "fstream.open(c:\\numbers.txt): error: can't open this file." << endl;
// output: fstream.open(c:\\numbers.txt): error: can't open this file.
file.close();
return 0;
}
It's no compiler error but the program can't open the text file. This file exists, but I can't open it from my program. I tried to move it to "C:/" and to the "<visual studio 2022 project folder>/x64/Debug" but it didn't help.
I tried to compile it with a g++ on Linux and it's all right there. In Windows I can open that file with Notepad, but my program can't open it. What can be wrong?
std::ofstream
to create/write a file with a unique, easily recognized name, something likeMyUniqueTestFile.txt
. Run the program, and, afterwards, use Windows Explorer to search for the file. The directory where you find it, is the directory where you should putnumbers.txt
._getcwd
function can be used to get the current working directory.