My codes work well In Dev c++ IDE, but in linux te

2019-09-21 08:20发布

while(true) {
   getline(myfile, a[i]);
   if (a[i]=="")//or if(a[i].empty())
        break;
    i++;
    n = i;
}

In this while loop, when getline function gets a blank line from myfile object (there is a blank line between a series of binary numbers).

Example:

101010
000
11
1
00
                <- when getline meets this line, by "if" break; has to work.
0011
10
00111
1101

But, it doesn't realize that blank line.

What is wrong?

What should I code to break when getline() meets the blank line?

I do this through PuTTY.

1条回答
Viruses.
2楼-- · 2019-09-21 09:06

You are most likely running into the NL/CR issue.

Instead of

if (a[i]=="")

Use something like:

if (isEmptyLine(a[i]))

where

bool isEmptyLine(std::string const& s)
{
   for ( auto c : s )
   {
      if ( !std::isspace(c) )
        return false;
   }
   return true;
}

You can also convert the file into a file with UNIX style line endings by using a utility called dos2unix. That should also fix the problem.

查看更多
登录 后发表回答