将文件中数据一次性全部读出并打印。
文件示例
文件名:json_str3.txt
文件内容:
{
"people": [
{
"firstName": "z",
"lastName": "Jason",
"email": "bbbb@126.com",
"height": 1.67
},
{
"lastName": "jadena",
"email": "jadena@126.com",
"age": 8,
"height": 1.17
},
{
"email": "cccc@126.com",
"firstName": "z",
"lastName": "Juliet",
"age": 36,
"height": 1.55
}
]
}
C语言程序实现
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
FILE *f;
int len;
char *data;
f=fopen("json_str3.txt","rb");
fseek(f,0,SEEK_END);
len=ftell(f);
fseek(f,0,SEEK_SET);
data=(char*)malloc(len+1);
fread(data,1,len,f);
fclose(f);
printf("data :\n%s\n", data);
return 0;
}