C语言文件读写

#include

#include

#define N 256

int main(){

char buff[N],file[20];

int lcnt;

FILE *fp; //文件型指针

puts("Input a flie name:\n");

scanf("%s",file);

if((fp=fopen(file,"r"))==NULL){ //以只读方式打开指定文件

printf("\nFile %s fail.",file);

exit(1); //读取失败退出运行

}

lcnt=0;

while(fgets(buff,N,fp)!=NULL){ //读取fp指向文件的字符串存放于buff

printf("%3d:%s",++lcnt,buff);

}

fclose(fp);

}

你可能感兴趣的:(C语言文件读写)