我有两个文本文件。如果其中一个文本文件为空,则它应该只显示其他文本文件。但即使它们都是空的,程序仍然会显示它们。这是我的功能代码。
void displayTextFile(FILE *fileptr, FILE *fileptr2)
{
char text[100];
char text2[100];
fileptr = fopen("textfile.txt", "r");
fileptr2 = fopen("textfile2.txt", "r");
if(fileptr == NULL && fileptr2 == NULL)
{
printf("Both text files do not exist");
getch();
return;
}
else
{
if(!feof(fileptr) && !feof(fileptr2))
{
rewind(fileptr);
fgets(text, 100, fileptr);
printf("\n\nThe contents of the first text file:\n%s", text);
fclose(fileptr);
rewind(fileptr2);
fgets(text2, 100, fileptr2);
printf("\n\nThe contents of the second text file:\n%s", text2);
fclose(fileptr2);
getch();
return;
}
else if(feof(fileptr2) && !feof(fileptr))
{
printf("\nThe second text file is empty\n");
rewind(fileptr);
fgets(text, 100, fileptr);
printf("\n\nThe contents of the first text file:\n%s", text);
fclose(fileptr);
getch();
return;
}
else if(feof(fileptr) && !feof(fileptr2))
{
printf("\nThe first text file is empty\n");
rewind(fileptr2);
fgets(text2, 100, fileptr2);
printf("\n\nThe contents of the first text file:\n%s", text2);
fclose(fileptr2);
getch();
return;
}
else
{
printf("Both text files are empty");
}
}
}