I'm opening a file and reading through it line by line, printing each line to the console. If I do not put some sort of newline character at the end of each call to cout, only one line is printed.
This code works as expeted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int main(int argc, char* argv[])
{
if (argc < 2)
return 0;
std::ifstream file(argv[1]);
if (!file.is_open())
{
std::cout<<"Problem opening file: " << argv[1] << "\n";
return 1;
}
std::string in;
while (std::getline(file,in))
{
std::cout<<in<<"\n";//with a new line
}
}
However, changing the cout in the while loop to look like this
std::cout<<in;
causes only one line of the file to be printed to the console.
Still works ok for me. The only way I can get just a single line (the last line) from the file is if I change the code in some way (such as adding an unwanted semicolon on line 17).
I had an idea. If you use a text file created on a windows system and try to use it on a non-windows system, the difference in line endings may cause strange behaviour,
If you use a text file created on a windows system and try to use it on a non-windows system, the difference in line endings may cause strange behaviour