What might be the problem? In this case, the file did not open, because
only applies to output files.
To find where the error occurred, add some tests of the file status. After every operation, possibly something could have gone wrong. So you need to check.
using namespace std;
cout << "Error 1 - could not open input file\n";
cout << "Error 2 - getline failed\n";
cout << "Error 3 - seekg failed\n";
cout << "4 - tellg position: " << test.tellg() << '\n';
The code as it stands outputs the message:
Error 1 - could not open input file
after the error message, if the file is not open, there's no point in continuing with the rest of the program.
So fix that code error now:
Recompile the code and run the program again.
If the file is still not open now, you need to check the name and location of the input file is correct.
When you get the file to open there may still be errors.
When I run it, this is the output:
Error 2 - getline failed
Error 3 - seekg failed
4 - tellg position: -1
Why did the getline fail? Well, the first line of my file is "apple". That needs a buffer big enough to contain the 5 letters plus the null terminator '\0'.
So, let's make the buffer bigger:
I made the buffer reasonably large, because the line length is often likely to be more than five characters.
Recompile and try again. If all goes well, you should see: