cout behaviour

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.

What causes this?
Last edited on
causes only one line of the file to be printed to the console.

Don't you mean it causes all of the lines of the file to be printed together on a single line?

getline() discards the newline which it reads from the file.
http://www.cplusplus.com/reference/string/string/getline/
Last edited on
I have a file test.txt that contains
line 1
line 2
line 3

Running the above code with the new line results in
line 1
line 2
line 3

Without the newline results in
line 3
Last edited on
That seems odd.

When I try it I get:
line 1
line 2
line 3
and
line 1line 2line 3

Did you change anything else apart from the "\n" ?

Maybe you could post your code which gives this output:

line 3

Here is exactly what I have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <string>

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;//<----
	}
}


Compiling with g++ -std=c++14 and gcc version 5.3.0 (GCC).
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,

As an experiment, you could try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>

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 << int (in.back()) << '\n';
        std::cout << in << '\n';
    }

}

With the same input file, this is the output I get:
49
line 1
50
line 2
51
line 3
Last edited on
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


This was it!
Topic archived. No new replies allowed.