Hello noblemin,
First you should provide the input file. That will let others know what it looks like and there could be something in the file that you may not see. It helps when everyone can use the same information.
You are using "std::string" and "std::getline" in the program, but you did not include the "string" header file to cover these. The compiler should have stopped with errors about these. If not you may need to turn up the warning level.
It also helps to mention which version of VS you are using, 2015,2017 or 2019.
I am not familiar with VS Code and how it works, but I am thinking it should work much the same as the VS IDEs.
On line 11 you open a file stream "fin", but how do you know it is open?
When you open a file for input it is a
MUST that you check it.
The following code will demonstrate that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
const std::string inFileName{ "text.txt" };
ifstream fin(inFileName);
//fin.open("text.txt");
if (!fin)
{
std::cerr << "\n File \"" << inFileName << "\" did not open!\n";
return 1;
}
string line;
while (getline(fin, line))
{
cout << line << '\n';
}
return 0; // <--- Not required, but makes a good break point.
}
|
Line 11 shows you how you can define the variable name and open the file at the same time. This is considered the preferred method, but if you want or need touse the "open" line 13 that is OK.
Lines 15 - 20 are what you need to check if the file stream is open and ready for use.
You do not need to initialize a "std::string".
1 2
|
string line = "";
string line;
|
Both of these are the same. They both produce a string with a zero size. Adding ( = "") has no advantage.
Andy
Edit: in regards to the last line this is based on what I see in VS2017.