如何读取文件并保存特定出现的字符串?
How to read a File and save specific occuring strings?
我需要读取一个文件,这是一个使用QT Framework的.asc文件。
我需要检查文件中的特定字符串,例如“(Name)”。
特定字符串之后的数字必须保存在另一个 .txt 文件中。
所以我尝试使用与 getline()
语句连接的 for 循环,而 if 语句应该检查是否已经找到预定义的字符串。
如果找到字符串,我将使用字符串变量 .assign()
方法将整个匹配行输入到另一个字符串。
但遗憾的是它仍然无法正常工作,可能存在逻辑错误,所以我请求你的帮助!
std::string line;
std::string search = "(Name)";
std::string content;
std::ifstream ifs("C:/PROG/EMS/LD1/analyse_led");
for(unsigned int curLine = 0; std::getline(ifs, line); curLine++) {
if (line.find(search) != std::string::npos) {
content.assign( (std::istreambuf_iterator<char>(line) ),
(std::istreambuf_iterator<char>() ) );
}
}
当然我实现了上面所有相关的包含文件,感谢您的帮助!
如果您需要更多信息,请在评论中告诉我。
content
和line
都是std::strings
,就做
content = line;
或
content = std::move(line);
如果您之后不需要对 line
执行任何其他操作。 (line
之后将为空)。
我不确定你想用 istreambuf_iterator
实现什么,但我确定它不会起作用...
我需要读取一个文件,这是一个使用QT Framework的.asc文件。
我需要检查文件中的特定字符串,例如“(Name)”。 特定字符串之后的数字必须保存在另一个 .txt 文件中。
所以我尝试使用与 getline()
语句连接的 for 循环,而 if 语句应该检查是否已经找到预定义的字符串。
如果找到字符串,我将使用字符串变量 .assign()
方法将整个匹配行输入到另一个字符串。
但遗憾的是它仍然无法正常工作,可能存在逻辑错误,所以我请求你的帮助!
std::string line;
std::string search = "(Name)";
std::string content;
std::ifstream ifs("C:/PROG/EMS/LD1/analyse_led");
for(unsigned int curLine = 0; std::getline(ifs, line); curLine++) {
if (line.find(search) != std::string::npos) {
content.assign( (std::istreambuf_iterator<char>(line) ),
(std::istreambuf_iterator<char>() ) );
}
}
当然我实现了上面所有相关的包含文件,感谢您的帮助! 如果您需要更多信息,请在评论中告诉我。
content
和line
都是std::strings
,就做
content = line;
或
content = std::move(line);
如果您之后不需要对 line
执行任何其他操作。 (line
之后将为空)。
我不确定你想用 istreambuf_iterator
实现什么,但我确定它不会起作用...