读一行:
#include<stdio.h> #include<string.h> #include<stdlib.h> const int maxn = 10; int main() { char s[1024] = {0}; FILE *p = fopen("/home/exbot/wangqinghe/C/20190716/file.txt","r"); //第一个参数是一个内存地址,第二个参数是这块内存的大小, //第三个参数是fopen返回的文件指针 fgets(s,maxn,p); printf(" s = %s\n",s); fclose(p); return 0; }
读所有:
#include<stdio.h> #include<string.h> #include<stdlib.h> const int maxn = 10; int main() { char s[1024] = {0}; FILE *p = fopen("/home/exbot/wangqinghe/C/20190716/file.txt","r"); //feof(p) p文件到尾部则返回正 while(!feof(p)) { memset(s,0,sizeof(s)); fgets(s,sizeof(s),p); printf("%s\n",s); } fclose(p); return 0; }