在Linux中,使用fread读取文件内容:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *fp;
fp = fopen("hello.txt","rt");
//求得文件的大小
fseek(fp,0,SEEK_END);
int size = ftell(fp);
printf("size:%d\n",size);
rewind(fp);
//申请一块能装下整个文件的空间
char *ar = (char *)malloc(sizeof(char)*size);
//读文件,每次读一个,共读size次
fread(ar,1,size,fp);
printf("%s\n",ar);
fclose(fp);
free(ar);
return 0;
}