0

I have a file which contain some numbers, all in one line . I would like to read this file and put this line to a string variable. So as it contains only one line, the getline() method should works only once

But It's not the case. It works twice. I notice that first my string_descriptor contains the number (so it's okey) but after getline take another line and this time it's empty but by looking at the debugger the string contains a lot of \O\ like 10 times.

\O\O\O\O\O\O\O\O\O\O\O\O\O\O\O\ 

enter image description here

And it bothers me because after I'm doing some processing and because of that my app crashs.

So what I'm doing is the following :

 fs.open (desc.c_str (), std::ios::in); string line; if(!fs.is_open()) { cout<<"\n Cannot open the text.txt file"; } else { std::string string_descriptor; while (!fs.eof ()) { getline( fs , line); if (line != "" && line.find_first_not_of(' ') != std::string::npos && !line.empty()) { string_descriptor = line; std::cout << "String descriptor : " << string_descriptor << std::endl; } } } 

So why it happened ? And especially how can I handle that ? I tried to handle that by doing the following but It's still the same :

if (line != "" && line.find_first_not_of(' ') != std::string::npos && !line.empty()) 

I checked my file and there is no space at the end of the file, so far I know.

Thank you for your help

4
  • 2
    You should not loop using eof(): stackoverflow.com/questions/5605125/… Commented Nov 25, 2016 at 14:10
  • Does your file contain the nul chars? Commented Nov 25, 2016 at 14:21
  • No only numbers so far I know Commented Nov 25, 2016 at 14:22
  • @stark there were an invisible space at the end of the line...Do you know how can i check that for my future processing ? Commented Nov 25, 2016 at 14:24

1 Answer 1

1

To avoid the second iteration of the loop change the loop

 while (!fs.eof ()) { getline( fs , line); //... 

the following way

 while ( getline( fs , line) ) { //... 

Also this condition

if (line != "" && line.find_first_not_of(' ') != std::string::npos && !line.empty()) 

can look more simpler

if ( line.find_first_not_of(' ') != std::string::npos ) 
Sign up to request clarification or add additional context in comments.

10 Comments

Thank for the modification but even by changing in this way but still got the same issue...
@lilouch Maybe the file contains two records and the last one is empty. What is the problem?
Actually, my file contains only one line with number and my line variable should contains only the numbers but it seems that it loops again and I got an empty string from line. I don't want that and that's why I used the IF condition...But I still get an empty string with full of \0\
@lilouch It means that the file contains more than one record and nothing more.
Thank for your answer but on my file, I have just one line and not two...that's the thing
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.