我有一个文件,我需要使用cin和命令行重定向来解析.第一个然而很多行由两个双字和两个字符串组成,然后是一个空白行,然后是更多信息.我需要停止读取此空行处的数据并切换到不同的变量,因为在此之后数据的格式将不同.如何在不丢失任何数据的情况下检测带有cin的空行?谢谢您的帮助...
像解析任何文件一样解析它,只记录空行发生的时间:
#include <sstream>
#include <fstream>
#include <string>
int main()
{
std::ifstream infile("thefile.txt", "rb");
std::string line;
while (std::getline(infile, line)) // or std::cin instead of infile
{
if (line == "") { break; } // this is the exit condition: an empty line
double d1, d2;
std::string s1, s2;
std::istringstream ss(line);
if (!(ss >> d1 >> d2 >> s1 >> s2)) { /* error */ }
// process d1, d2, s1, s2
}
// move on
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3071 次 |
| 最近记录: |