My codes work well In Dev c++ IDE, but in linux terminal, it doesn't. (especially in the part of 'while' loop.) [closed]

只谈情不闲聊 提交于 2019-12-13 09:23:37

问题


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:


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.



来源:https://stackoverflow.com/questions/36157584/my-codes-work-well-in-dev-c-ide-but-in-linux-terminal-it-doesnt-especiall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!