我需要模拟grep-f命令在两个文件之间的效果 编译后的代码没有错误,但当我运行它而不是输出时,它显示了分段错误(核心转储) 我怎么能这么做呢?另外,如果您能找到一种更好的方法来打开f1和f2,而不使用AUX1和AUX2来提供文件名,那就更好了.先谢谢你.
//我知道我必须判断代码是否有足够的参数,如果没有,我应该对stderr做一些事情(这是我在课堂上被告知要做的,但我不知道如何做)
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *file1;
FILE *file2;
char aux1[100]="",aux2[100]="";
strcpy(aux1,argv[1]);
strcpy(aux2,argv[2]);
file1 = fopen("aux1","r");
file2 = fopen("aux2","r");
char s1[100]="",s2[100]="";
char aux[100]="";
while(fscanf(file1,"%s",aux)!=EOF)
{
strcat(s1,aux);
strcat(s1," ");
}
s1[strlen(s1)-1]='\0';
while(fscanf(file2,"%s",aux)!=EOF)
{
strcat(s2,aux);
strcat(s2," ");
}
s2[strlen(s2)-1]='\0';
printf("%s\n",strstr(s2,s1));
return 0;
}