C语言文件操作:从基础到实践
1. 文件行比较程序
在处理文件时,有时需要比较两个文件的内容。下面的程序可以逐行比较两个文件,并输出它们的第一行相同内容。若没有相同行,则会给出相应提示。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int read_text(char str[], int size, int flag);
int main(void)
{
FILE *fp1, *fp2;
char flag, str1[100], str2[100];
printf("Enter first file: ");
read_text(str1, sizeof(str1), 1);
fp1 = fopen(str1, "r");
if(fp1 == NULL)
{
printf("Error: fopen() for %s failed\n", str1);
exit(EXIT_FAILURE);
}
printf("Enter second file: ");
read_text(str1, sizeof(str1), 1);
fp2 = fopen(str1, "r");
if(fp2 == NULL)
{
fclose(fp1);
printf("Error: fopen() for %s failed\n", str1);
exit(EXIT_FAIL
超级会员免费看
订阅专栏 解锁全文
1162

被折叠的 条评论
为什么被折叠?



