std::getline ignores tabulator

Hi,

I searched/googled for some time now, but found nothing regarding this problem:
When reading lines from a file with getline(), all tabulators are missing.
A line looks like this:
a b c
(that is: a tabulator b tabulator c)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <iostream>
#include <fstream>

main()
{
	std::ifstream file;
	file.open("file.txt");   
	if(! file.is_open())
		return 1;
	std::string s;

	std::getline(file,s); // first line is: "a\tb\tc"
        std::cout << s << std::endl;  // prints: abc
}


The extracted string only contains the non-tab characters.
Expected result is: "a b c".
With a file with spaces instead of tabulators it works fine, but it should work with tabulators as well, right?

regards, klando

PS: using win7, MSVC++2010 Professional
Last edited on
> but it should work with tabulators as well, right?

Yes, it should.
http://ideone.com/WgV1XA
Ok, at least I'm sure now it should work.
Alas, it doesn't.
I run your code and again after the getline() the string contains no tabulators.
Therefore I'm fairly sure there is nothing wrong with the file (since the behaviour is the same whether I apply getline() to the ifstream or to the stringstream).

Any ideas what else could be the problem?
What does this print out?
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <sstream>
#include <string>

int main()
{
    std::istringstream stm( "a\tb\tc\nanother_line\n" ) ;
    char c ;
    stm >> std::noskipws ;
    while( stm >> c ) if( c == '\t' ) std::cout << "tab\n" ;
}


I don't have MSVC++ 2010, but just tested it on MSVC 2012 where it (the earlier code) works as expected.
Last edited on
That produces "tab" twice. As it should, I reckoned.

And now, after altering my code at several lines, it works. Still not sure why and what exactly did make the difference. Will be back to post the solution as soon as I know what it was.

Thanks JLBorges!

Topic archived. No new replies allowed.