gpt4 book ai didi

c - 尝试打印文件中的字符但打印空白

转载 作者:太空宇宙 更新时间:2023-11-04 03:30:27 24 4
gpt4 key购买 nike

这是我的一个小方法:

void CompareTwoFiles(FILE *fp1, FILE *fp2){

char ch1, ch2;
int flag = 0;
//must seek first to determine the sizes
fseek (fp1, 0, SEEK_END);
fseek (fp2, 0, SEEK_END);
if(ftell(fp1) != ftell(fp2)){
printf("The sizes of the files are different so they cannot be equal.\n");
return;
}
while (((ch1 = fgetc(fp1)) != EOF) &&((ch2 = fgetc(fp2)) != EOF))
{
/*
* character by character comparision
* if equal then continue by comparing till the end of the files
*/
if (ch1 == ch2)
{
flag = 1;
continue;
}
//If not equal then return the byte position
else
{
fseek(fp1, -1, SEEK_CUR);
flag = 0;
break;
}
}

if (flag == 0)
{
printf("Two files are not equal : byte position at which two files differ is %d.\n", ftell(fp1)+1);
printf("First file contains %c and second file contains %c \n", ch1, ch2); //ISSUE: prints blank for ch1 and ch2
}
else
{
printf("Two files are Equal\n ", ftell(fp1)+1);
}

}

我只想在我的 printf 中打印我用 fgetc 分配的两个字符。但是我却一片空白。输出如下所示:

第一个文件包含,第二个文件包含

有人可以指出我哪里出了问题吗?我对 C 和 C++ 有点生疏。

最佳答案

首先修复警告。

$ gcc main.c 
main.c: In function ‘CompareTwoFiles’:
main.c:37:12: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=]
printf("Two files are not equal : byte position at which two files differ
^
main.c:42:12: warning: too many arguments for format [-Wformat-extra-args]
printf("Two files are Equal\n ", ftell(fp1)+1);

进行干净的编译并倒带文件,您会看到您的代码有效。您的代码没有倒带,因此您不知道得到的是哪个 char

#include <stdio.h>

void CompareTwoFiles(FILE *fp1, FILE *fp2) {

char ch1, ch2;
int flag = 0;
//must seek first to determine the sizes
fseek(fp1, 0, SEEK_END);
fseek(fp2, 0, SEEK_END);
if (ftell(fp1) != ftell(fp2)) {
printf("The sizes of the files are different so they cannot be equal.\n");
return;
}
rewind(fp1);
rewind(fp2);
while (((ch1 = fgetc(fp1)) != EOF) && ((ch2 = fgetc(fp2)) != EOF)) {
/*
* character by character comparision
* if equal then continue by comparing till the end of the files
*/
if (ch1 == ch2) {
flag = 1;
continue;
}
//If not equal then return the byte position
else {
fseek(fp1, -1, SEEK_CUR);
flag = 0;
break;
}
}

if (flag == 0) {
printf("Two files are not equal : byte position at which two files differ is %d.\n", (int) ftell(fp1) + 1);
printf("First file contains %c and second file contains %c \n", ch1, ch2); //ISSUE: prints blank for ch1 and ch2
}
else {
printf("Two files are Equal %d\n ", (int) ftell(fp1) + 1);
}

}


int main() {

FILE *fp;
fp = fopen("tmp.txt", "r");
FILE *fp2;
fp2 = fopen("tmp2.txt", "r");

CompareTwoFiles(fp, fp2);
return 0;
}

tmp.txt

1 1 23 2134 123 12321

123

42

tmp2.txt

0 1 23 2134 123 12321

123

42

输出

$ ./a.out 
Two files are not equal : byte position at which two files differ is 1.
First file contains 1 and second file contains 0

关于c - 尝试打印文件中的字符但打印空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37107429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com