I want to read a file line by line. Did something like
void Parse (string filepath) {
ifstream sourceFile;
sourceFile.open(filepath);
for (string line; getline(sourceFile, line);) {
cout << "1" << endl;
cout << line << endl;
}
}
int main() {
Parse("C:\\test.txt");
getchar();
return 0;
}
Then put some text into C:\test.txt, but when I run, I dont get anything. Why? Not even the “1”. I notice no exception if the file is not there too. I suppose that a sign of a problem?
I think you have problems opening the file. I would suggest two things:
if (sourceFile))EDIT: adding the actual solution to the problem in my answer(instead of just a comment) so that people won’t miss it:
Here is one more thought – check the file name in its properties. Has happened to me that if windows hides the extension of the file the name is actually test.txt.txt, while what I see displayed is only test.txt.