【发布时间】:2016-04-18 12:18:44
【问题描述】:
我需要一个 C++ 代码来解决以下问题: 我有一个要从特定行开始读取的文本文件,然后我需要打印位于字符之间的输出 --- 例如:你好 我希望输出是你好
我认为我应该使用文本解析器,但不确定如何使用!
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
std::string line_;
ifstream file_("tty.txt");
if (file_.is_open())
{
while (getline(file_, line_))
{
std::cout << line_ << '\n';
}
file_.close();
}
else
std::cout << "error" << '\n';
std::cin.get();
system("PAUSE");
return 0;
}
【问题讨论】:
-
是的,您似乎确实需要解析器。为了解析文本,您尝试做什么?
-
可能是
std::regex之类的东西? -
看不到描述和示例字符串与预期输出之间的关系。您需要在两个
<\s>内添加文本还是什么? -
我的第一个想法也是正则表达式。
-
如果分隔符是简单的文本,只需搜索它们。
std::string可以很简单地做到这一点;不需要正则表达式的开销和复杂性。