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

lilouch
  • 934
  • 3
  • 18
  • 38

1 Answers1

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 )
Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
  • Thank for the modification but even by changing in this way but still got the same issue... – lilouch Nov 25 '16 at 14:15
  • @lilouch Maybe the file contains two records and the last one is empty. What is the problem? – Vlad from Moscow Nov 25 '16 at 14:17
  • 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 Nov 25 '16 at 14:18
  • @lilouch It means that the file contains more than one record and nothing more. – Vlad from Moscow Nov 25 '16 at 14:19
  • Thank for your answer but on my file, I have just one line and not two...that's the thing – lilouch Nov 25 '16 at 14:20
  • @lilouch You should check the file in binary representation. It can contain for example last symbol 1Ah. – Vlad from Moscow Nov 25 '16 at 14:21
  • Indeed there were an invisible space in the file :/. But do you have an idea how can i check that for my future processing ? – lilouch Nov 25 '16 at 14:23
  • @lilouch This does not create a problem for the file processing. – Vlad from Moscow Nov 25 '16 at 14:25
  • Actually yes because after my split my string by using the ' ' delim to put every number to a vector of string and then I convert each number from string to float. But as the end of my line contain a big nul string, it cannot convert the string and crashed... – lilouch Nov 25 '16 at 14:26
  • @lilouch The problem is not related with the file. The problem is in your code where you evidently incorrectly process lines. – Vlad from Moscow Nov 25 '16 at 14:28