gpt4 book ai didi

c++ - 如果一行包含某个 Word 保存它

转载 作者:行者123 更新时间:2023-11-28 08:10:46 24 4
gpt4 key购买 nike

#include <iostream>
#include <fstream>

using namespace std;

int main()
{

ifstream stream1("source.txt");
string line ;
ofstream stream2("target.txt");

while( std::getline( stream1, line ) )
{
stream2 << line << endl;
cout << line << endl;
}


stream1.close();
stream2.close(); return 0;
}

我怎样才能让这段代码使它在一行中找到单词,例如“HELLO”,它会将整行保存到 stream2 中?如果没有该词,则其余行将不会保存到 stream2。

基本上,如果它在阅读时在一行中找到单词“HELLO”。它输出它。如果没有,则跳过该行。

最佳答案

while( std::getline( stream1, line ) )
{
if(line.find("hello") != string::npos)
stream2 << line << endl;

cout << line << endl;
}

关于c++ - 如果一行包含某个 Word 保存它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9139353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com