j | e | s | u | i | s | u | n | p | r | o | g | r | a | m | m | e | u | r | '\0' |
#include
#include
#include <字符串>
#include
using 命名空间 std;
void searchInFile(char string[])
{
ifstream myStream(C:/test.txt”);
if(myFlow)
{
int i=0,position=0;
//按字符读取
myFlux.clear();
//初始化 0
//ios::beg: start from start
myFlux.seekg(0, ios::beg);
char c;
//只要不是文件的末尾
while(myStream.get(c))
{
//增加播放头的位置
position++;
//如果字符与
的字符匹配 ///在 i
//然后移动到下一个字符
if(c==string[i]){
i++;
//如果字符串中的所有字符都
//选中,那么我们在
//文件中找到字符串
if(i==strlen(string)) //strlen: 返回字符串的长度
printf(搜索到的字符串在位置: %d ”
并在位置 %d\n, position-strlen(字符串),position);
}
//否则如果我们找到一个不匹配的字符
///不匹配 then
//从 0
else
i=0 重新启动;
}
}
else
printf(无法打开文件\n”);
}
int main()
{
char string[]=程序员”;
searchFile(字符串);
system(暂停”);
}
Please disable your ad blocker and refresh the window to use this website.