我的代码在 Dev c++ IDE 中运行良好,但在 linux 终端中却不行。 (特别是在 'while' 循环的部分。)
My codes work well In Dev c++ IDE, but in linux terminal, it doesn't. (especially in the part of 'while' loop.)
while(true) {
getline(myfile, a[i]);
if (a[i]=="")//or if(a[i].empty())
break;
i++;
n = i;
}
在这个while循环中,当getline
函数从myfile
对象中得到一个空行时(一系列二进制数之间有一个空行)。
示例:
101010
000
11
1
00
<- when getline meets this line, by "if" break; has to work.
0011
10
00111
1101
但是,它没有意识到那个空行。
怎么了?
getline()
遇到空行应该怎么打断?
我通过 PuTTY 做到这一点。
您很可能 运行 遇到了 NL/CR 问题。
而不是
if (a[i]=="")
使用类似于:
if (isEmptyLine(a[i]))
其中
bool isEmptyLine(std::string const& s)
{
for ( auto c : s )
{
if ( !std::isspace(c) )
return false;
}
return true;
}
您还可以使用名为 dos2unix
的实用程序将文件转换为具有 UNIX 样式行结尾的文件。这也应该可以解决问题。
while(true) {
getline(myfile, a[i]);
if (a[i]=="")//or if(a[i].empty())
break;
i++;
n = i;
}
在这个while循环中,当getline
函数从myfile
对象中得到一个空行时(一系列二进制数之间有一个空行)。
示例:
101010
000
11
1
00
<- when getline meets this line, by "if" break; has to work.
0011
10
00111
1101
但是,它没有意识到那个空行。
怎么了?
getline()
遇到空行应该怎么打断?
我通过 PuTTY 做到这一点。
您很可能 运行 遇到了 NL/CR 问题。
而不是
if (a[i]=="")
使用类似于:
if (isEmptyLine(a[i]))
其中
bool isEmptyLine(std::string const& s)
{
for ( auto c : s )
{
if ( !std::isspace(c) )
return false;
}
return true;
}
您还可以使用名为 dos2unix
的实用程序将文件转换为具有 UNIX 样式行结尾的文件。这也应该可以解决问题。