繁体   English   中英

如何检测C ++中是否遇到换行符?

[英]How to detect if a newline is encountered in C++?

我输入的内容使我们知道当前的测试用例已经完成,接下来要用介于两者之间的空行开始。 如何检查C ++中是否出现空白行?

有很多检测空白行的方法(当然,如果该行不包含空格)。

示例1(假设您正在逐行获取数据):

#include <string.h>

...

if (strcmp(yourLine, "") == 0) { // or strcmp(yourLine, "\n"), it depends how you get yourLine in your code above
    // your code
    ...
}

范例2:

#include <string>

std::string yourData = "qwe\nrty\n\nasd\nfgh\n";

std::size_t index;
while ((index = yourData.find("\n\n")) != std::string::npos) {
    std::string part = yourData.substr(0, index);
    // your code
    yourData = yourData.substr(index);
}

在示例2中,使用了方法std :: string :: find和std :: string :: substr。 您可以查看它们的文档: http : //www.cplusplus.com/reference/string/string/find/,http : //www.cplusplus.com/reference/string/string/substr/

您将需要检查“ \\ r”,“ \\ n”或“ \\ r \\ n”,具体取决于您的代码所运行的操作系统。 看到\\ n和\\ r之间的区别? 很好地解释了为什么这取决于操作系统。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM