Step 2: check your file position.
Assuming your project name is “catsanddogs”, on your hd there should be a directory named “catsanddogs” which should contain a “catsanddogs.pro” file
and
another directory named “build-catsanddogs…”
where you should find both a “debug” and a “release” directories.
What is inside your actual if statement? There must be something.
If test.txt is an existing file, do ifstream instead of fstream.
And, do:
1 2 3 4 5 6 7 8
if (file)
{
cout << "FILE OPENED\n";
}
else
{
cout << "FILE NOT OPENED\n";
}
If it's not opening the file, then do the aforementioned troubleshooting steps.
On mac/linux/unix you can do system("pwd"); to have it print the current working directory. On Windows, you can do system("cd"); to have it print the current working directory.
Put the test.txt in that folder.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
fstream file;
file.open("/Users/againtry/test.txt");
string line;
cout << true << ' ' << file.good() << '\n';
if (file.good() == true)
{
getline(file, line);
cout << line << '\n';
cout << "File is good\n";
}
else
cout << "File is not good\n";
cout << "Hello World!" << endl;
return 0;
}
/Users/againtry/qtstuff_and_nonsense/build-untitled-Desktop_Qt_5_14_0_clang_64bit-Debug/untitled ; exit;
1 1
First line of text
File is good
Hello World!