Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较c语言中不同文本文件中的字符串_C_String - Fatal编程技术网

比较c语言中不同文本文件中的字符串

比较c语言中不同文本文件中的字符串,c,string,C,String,我有两个文本文件test1.txt和test2.txt。它们每个都有多个字符串。如果它们相同或不同,我会比较它们并打印出来。 我的代码如下: #include<stdio.h> #include<stdlib.h> #include<string.h> int main(){ char temp[100]; char const *t1; char const *t2; FILE *fp1=fopen("test1.txt","r"); while((t1

我有两个文本文件test1.txt和test2.txt。它们每个都有多个字符串。如果它们相同或不同,我会比较它们并打印出来。 我的代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char temp[100];

char const *t1;
char const *t2;

FILE *fp1=fopen("test1.txt","r");
while((t1=fgets(temp,sizeof(temp),fp1))!=NULL){
     FILE *fp2=fopen("test2.txt","r");
     while((t2=fgets(temp,sizeof(temp),fp2))!=NULL){
          if(strcmp(t1,t2)==0){
             printf("same\n");
             }
           else{
             printf("Differ\n");
             }
          }
     fclose(fp2);
     }
fclose(fp1);
}
test2.txt:

10101001
1001
上述代码提供以下输出:

same
same
same
same
这显然是错误的

我做错了什么?如何修复它

更新

我修正了密码。以下代码工作正常,但请告诉我是否存在更好的解决方案:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char temp1[100];
char temp2[100];

char const *t1;
char const *t2;

FILE *fp1=fopen("test1.txt","r");
while((t1=fgets(temp1,sizeof(temp1),fp1))!=NULL){
     FILE *fp2=fopen("test2.txt","r");
     while((t2=fgets(temp2,sizeof(temp2),fp2))!=NULL){
          if(strcmp(t1,t2)==0){
             printf("same\n");
             }
           else{
             printf("Differ\n");
             }
          }
     fclose(fp2);
     }
fclose(fp1);
}
#包括
#包括
#包括
int main(){
char temp1[100];
char-temp2[100];
字符常量*t1;
字符常量*t2;
文件*fp1=fopen(“test1.txt”、“r”);
而((t1=fgets(temp1,sizeof(temp1),fp1))!=NULL){
文件*fp2=fopen(“test2.txt”、“r”);
而((t2=fgets(temp2,sizeof(temp2),fp2))!=NULL){
if(strcmp(t1,t2)==0){
printf(“相同的\n”);
}
否则{
printf(“不同”\n);
}
}
fclose(fp2);
}
fclose(fp1);
}
试试这个:

include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
        char temp[100];

        char t1[100];
        char t2[100];

        FILE *fp1=fopen("test1.txt","r");
        FILE *fp2=fopen("test2.txt","r");

        while((fgets(t1,100,fp1)!= NULL) && (fgets(t2,100,fp2)!= NULL))
        {

                if(strcmp(t1, t2) == 0)
                {
                        printf("same\n");
                }
                else
                {
                        printf("NOT same\n");
                }
        }

}
包括
#包括
#包括
int main()
{
炭温度[100];
chart1[100];
chart2[100];
文件*fp1=fopen(“test1.txt”、“r”);
文件*fp2=fopen(“test2.txt”、“r”);
而((fgets(T1100,fp1)!=NULL)和&(fgets(T2100,fp2)!=NULL))
{
if(strcmp(t1,t2)==0)
{
printf(“相同的\n”);
}
其他的
{
printf(“不相同的\n”);
}
}
}
不要在while循环中使用t1 当f1不等于null时,将f1中的第一个字符串复制到temp1 类似地,当f2.txt不等于null时,将f2中的第一个字符串cipy改为temp2 现在使用strcmp(temp1,temp2)比较字符串temp1和temp2
执行此操作,直到f1.txt或f2.txt等于null

您将一行从f1读入temp。然后,将f2中的一行读入temp。然后检查temp和temp是否包含相同的字符串,这显然是因为它们是相同的变量。是的,我刚刚意识到并更改了代码(更新)。fgets(T11000,fp1),为什么1000比100。谢谢你的建议!未使用变量char temp[100]。此外,最好使用sizeof(t1)而不是100作为常量。请详细说明您的答案。。如果其他人遇到同样的问题,这会有所帮助。
include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
        char temp[100];

        char t1[100];
        char t2[100];

        FILE *fp1=fopen("test1.txt","r");
        FILE *fp2=fopen("test2.txt","r");

        while((fgets(t1,100,fp1)!= NULL) && (fgets(t2,100,fp2)!= NULL))
        {

                if(strcmp(t1, t2) == 0)
                {
                        printf("same\n");
                }
                else
                {
                        printf("NOT same\n");
                }
        }

}