C++ 如何使用C+检查.text文件中的字符序列+;?

C++ 如何使用C+检查.text文件中的字符序列+;?,c++,file,C++,File,我有一个.txt文件,其中包含连续的字符序列(没有空格)。我想在.txt文件中搜索一系列字符,但在这方面遇到了困难 ifstream fin; fin.open("sample.txt"); while (!fin.eof()) { ch = getchar(); if (ch == 'p' && ch + 1 == 'o' && ch + 2 == 'w') std::cout &l

我有一个.txt文件,其中包含连续的字符序列(没有空格)。我想在.txt文件中搜索一系列字符,但在这方面遇到了困难

ifstream fin;
    fin.open("sample.txt");
    while (!fin.eof())
    {
        ch = getchar();
        if (ch == 'p' && ch + 1 == 'o' && ch + 2 == 'w')
            std::cout << "Sequence found" << "\n";
    }
    fin.close();
ifstream-fin;
fin.open(“sample.txt”);
而(!fin.eof())
{
ch=getchar();
如果(ch='p'&&ch+1='o'&&ch+2='w')

std::cout这不起作用,
ch
是你读入的字符,但是
ch+1
不是下一个字符(你还没有读入)。它只是
ch
增加了
1
,所以这是字母表中的下一个字母,数字大一点,等等。这取决于
ch
是什么

如果您只想查看序列是否在文件中,那么我会将文件读入
std::string
中,如所示:


考虑下面的程序:<代码> int(){char c=‘p’;STD::CUT不相关,但仍然是代码中的一个主要问题:需要创建字节缓冲区(例如字节数组),然后调用<代码> .Read(缓冲区,缓冲区大小)函数在<代码> FIN<代码>中读取文件到缓冲区。
std::string slurp(std::ifstream& in) {
    std::stringstream sstr;
    sstr << in.rdbuf();
    return sstr.str();
}
if (myString.find("pow") != std::string::npos) {
    std::cout << "found!" << '\n';
}