cout behaviour

Feb 28, 2016 at 9:22pm
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 Feb 29, 2016 at 12:10am
Feb 28, 2016 at 10:28pm
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 Feb 28, 2016 at 10:32pm
Feb 29, 2016 at 12:08am
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 Feb 29, 2016 at 12:09am
Feb 29, 2016 at 12:15am
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

Feb 29, 2016 at 1:20am
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).
Feb 29, 2016 at 2:07am
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).

Feb 29, 2016 at 2:10am
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 Feb 29, 2016 at 2:34am
Feb 29, 2016 at 2:24am
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.