测试程序的目的是更新已有的json文件。
下面是测试程序的代码
#include "smartlight.h"
#include "cJSON.h"
cJSON *dofile(char *filename)
{
FILE *f;
long len;
char *data;
cJSON *json,*ret;
f=fopen(filename,"rb");
fseek(f,0,SEEK_END);
len=ftell(f);
fseek(f,0,SEEK_SET);
data=(char*)malloc(len+1);
fread(data,1,len,f);
data[len]='\0';
json=cJSON_Parse(data);
if (!json)
{
printf("Error before: [%s]\n",cJSON_GetErrorPtr());
ret = NULL;
goto EXIT;
}
else
{
//printf("%s\n",data);
ret = json;
}
EXIT:
free(data);
fclose(f);
return ret;
}
int write_file(char *filename,char *out)
{
FILE *fp = NULL;
fp = fopen(filename,"a+");
if(fp == NULL)
{
fprintf(stderr,"open file failed\n");
exit(-1);
}
fprintf(fp,"%s",out);
if(fp != NULL)
fclose(fp);
}
int main()
{
cJSON *root,*basicpara;
char *out;
root