[英]How to detect if a newline is encountered in C++?
I have input where we get to know that the current testcase has finished and next started by a blank line in between. 我输入的内容使我们知道当前的测试用例已经完成,接下来要用介于两者之间的空行开始。 How can I check that if a blank line has occurred in C++?
如何检查C ++中是否出现空白行?
There're many ways to detect a blank line (if the line doesn't contain spaces, of course). 有很多检测空白行的方法(当然,如果该行不包含空格)。
Example 1 (it's supposed you're getting your data line by line): 示例1(假设您正在逐行获取数据):
#include <string.h>
...
if (strcmp(yourLine, "") == 0) { // or strcmp(yourLine, "\n"), it depends how you get yourLine in your code above
// your code
...
}
Example 2: 范例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);
}
In Example 2 methogs std::string::find and std::string::substr were used. 在示例2中,使用了方法std :: string :: find和std :: string :: substr。 You can look at the documentation for them: http://www.cplusplus.com/reference/string/string/find/ , http://www.cplusplus.com/reference/string/string/substr/
您可以查看它们的文档: http : //www.cplusplus.com/reference/string/string/find/,http : //www.cplusplus.com/reference/string/string/substr/
You will need to check for '\\r', '\\n', or "\\r\\n" depending on what OS your code is running on. 您将需要检查“ \\ r”,“ \\ n”或“ \\ r \\ n”,具体取决于您的代码所运行的操作系统。 See Difference between \\n and \\r?
看到\\ n和\\ r之间的区别? for a good explanation on why this is OS-dependent.
很好地解释了为什么这取决于操作系统。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.