与字符串比对时,要给字符串后边 strcat上 \n
在这里插入代码片
#include <stdio.h>
#include <stdlib.h>
#define LINE 1024
char *ReadData(FILE *fp, char *buf)
{
return fgets(buf, LINE, fp);//读取一行到buf
}
void someprocess(char *buf)
{
printf("%s", buf);//这里的操作你自己定义
}
int main()
{
FILE *fp;
char *buf, filename[20], p;
printf(“input file name:”);
gets(filename);
if ((fp = fopen(filename, “r”)) == NULL) {
printf(“open file error!!\n”);
return;
}
buf = (char)malloc(LINE * sizeof(char));
while (1) {
p = ReadData(fp, buf);//每次调用文件指针fp会自动后移一行
if (!p)//文件读取结束则跳出循环
break;
someprocess(buf);
}
return 0;
}