在C语言编程中从多个文件中搜索字符串的程序
下面是代码和整个设置的链接:我在添加搜索代码的同时对以前的代码做了一些调整。 这将打开正确的文件,一次读取1个字符,直到找到单词,然后移动到下一个文件在C语言编程中从多个文件中搜索字符串的程序,c,string,file,search,C,String,File,Search,下面是代码和整个设置的链接:我在添加搜索代码的同时对以前的代码做了一些调整。 这将打开正确的文件,一次读取1个字符,直到找到单词,然后移动到下一个文件 PS F:\PROGRAMMING\stuff\SSE> .\FileHandle1.exe the test.text test2.txt could not open the file #包括 #包括 #包括 void locate(int argc,char*argv[]){ 对于(int n=2;n0){ if(strstr(缓冲
PS F:\PROGRAMMING\stuff\SSE> .\FileHandle1.exe the test.text test2.txt
could not open the file
#包括
#包括
#包括
void locate(int argc,char*argv[]){
对于(int n=2;n0){
if(strstr(缓冲区,argv[1])){
printf(“%s”位于%s\n,argv[1],argv[n]);
打破
}否则{
}
指针+=偏移量;
}
fclose(文件);
}否则{
printf(“无法打开文件%s\n”,argv[n]);
}
}
}
int main(int argc,char*argv[]){
如果(argc@Nina尝试过,但它甚至不会显示第一个文件的结果:(好的,让我看看它可能会使系统帮助您:fp1=fopen(argv[i+1],“r”);if(fp1==NULL){perror(argv[i+1]);…
如果您使用一致的缩进和格式,就像我刚才对代码所做的那样,您至少会看到一个问题:您试图在循环中打开所有文件,而不读取中间的每个文件。因此只会搜索最后一个文件。--但这不是错误。因为只有在fopen()时才会出现此消息
失败,我假设“test2.txt”不存在。哦,你在结尾处发布的链接指向“localhost”,这是每个人的本地计算机。这肯定不是你的意思。在什么意义上?它不是打开多个文件还是没有找到你的单词?你不清楚你的错误是什么。事实上,我的argv[1]将是我要搜索的字符串和我的argv[2]等我的文件,我想在其中执行搜索操作…上面你给出的解决方案是帮助我成功地打开文件,没有错误..但给我看这个ERO或PS F:\PROGRAMMING\stuff\SSE>/FileHandle1.exe test.txt test2.txt抱歉,无法完成d一个匹配项。你没有给程序一个词来搜索。你只给了它文件名。@是的,谢谢,我以前没有注意到,但仍然是PS F:\PROGRAMMING\stuff\SSE>/FileHandle1.exe test.txt test2.txt无法打开文件the@SamarthShinde看看调整后的公司它现在应该可以按预期工作了。
PS F:\PROGRAMMING\stuff\SSE> .\FileHandle1.exe the test.text test2.txt
could not open the file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void locate(int argc,char* argv[]){
for(int n = 2; n < argc;++n){
FILE* file = fopen(argv[n], "r");
if(file){
printf("opened %s\n",argv[n]);
//insert your word search here
char buffer[1024] = {0};
char* pointer = buffer;
size_t offset = 0;
while((offset = fread(pointer, 1, 1, file))>0){
if(strstr(buffer, argv[1])){
printf("%s found in %s\n",argv[1],argv[n]);
break;
}else{
}
pointer+=offset;
}
fclose(file);
}else{
printf("Failed to open file %s\n",argv[n]);
}
}
}
int main(int argc,char* argv[]){
if(argc<3){
return -1;
}
locate(argc, argv);
return 0;
}