将数据追加到C中的json文件中:追加在达到一定数量的条目后停止
目标是将条目附加到将数据追加到C中的json文件中:追加在达到一定数量的条目后停止,c,C,目标是将条目附加到json文件中。这是一个与我的应用程序类似的演示程序。当我运行下面的代码时,在追加1020个条目后,追加停止 #include <stdio.h> #include <string.h> #define ENTRIES_PER_LOG 10000 static int packet_count = 0; static char filename[100] = "output.json"; int write_json(const
json
文件中。这是一个与我的应用程序类似的演示程序。当我运行下面的代码时,在追加1020个条目后,追加停止
#include <stdio.h>
#include <string.h>
#define ENTRIES_PER_LOG 10000
static int packet_count = 0;
static char filename[100] = "output.json";
int write_json(const char *json){
packet_count++;
// create file if it doesn't exist
FILE* fp = fopen(filename, "r");
if (!fp)
{
fp = fopen(filename, "w");
if (!fp)
return 0;
fputs("[]", fp);
fclose(fp);
}
// add the document to the file
fp = fopen(filename, "rb+");
if (fp)
{
// check if first is [
fseek(fp, 0, SEEK_SET);
if (getc(fp) != '[')
{
fclose(fp);
return 0;
}
// is array empty?
int is_empty = 0;
if (getc(fp) == ']')
is_empty = 1;
// check if last is ]
fseek(fp, -1, SEEK_END);
if (getc(fp) != ']')
{
fclose(fp);
return 0;
}
// replace ] by ,
fseek(fp, -1, SEEK_END);
if (!is_empty)
fputc(',', fp);
// append the document
fputs(json, fp);
// close the array
fputc(']', fp);
fclose(fp);
return 1;
}
return 0;
}
int main(){
int i;
for(i = 0; i < 10000; i++){
char json[100] = "";
sprintf(json, "{\"a\":%d}", packet_count);
printf("%s\n", json);
write_json(json);
}
}
#包括
#包括
#根据日志10000定义条目
静态int数据包计数=0;
静态字符文件名[100]=“output.json”;
int write_json(const char*json){
数据包计数++;
//创建不存在的文件
FILE*fp=fopen(文件名,“r”);
如果(!fp)
{
fp=fopen(文件名,“w”);
如果(!fp)
返回0;
FPUT(“[]”,fp);
fclose(fp);
}
//将文档添加到文件中
fp=fopen(文件名为“rb+”);
if(fp)
{
//检查第一个是否正确[
fseek(fp,0,SEEK_集);
如果(getc(fp)!='[')
{
fclose(fp);
返回0;
}
//数组是否为空?
int为_空=0;
如果(getc(fp)=']')
为空=1;
//检查最后一个是否为]
fseek(fp,-1,SEEK_END);
如果(getc(fp)!=']')
{
fclose(fp);
返回0;
}
//替换为,
fseek(fp,-1,SEEK_END);
如果(!为空)
fputc(',,fp);
//附加文档
fputs(json,fp);
//关闭阵列
fputc(']',fp);
fclose(fp);
返回1;
}
返回0;
}
int main(){
int i;
对于(i=0;i<10000;i++){
char json[100]=“”;
sprintf(json,“{\'a\':%d}”,数据包计数);
printf(“%s\n”,json);
编写json(json);
}
}
在每次运行中,它在
json
文件中追加1020个条目后停止。但是程序一直运行到for循环结束。重新启动进程会开始向文件追加内容,但它只追加1020个条目,然后停止追加内容。我不知道为什么会停止。任何关于为什么在一定数量的条目后追加停止的帮助或说明都将不胜感激。您的程序正在泄漏资源。也就是说,每次调用write_json
都会打开文件两次,同时泄漏第一次打开的描述符。经过一定数量的调用后,它已达到允许打开的文件的限制
// create file if it doesn't exist
FILE* fp = fopen(filename, "r"); // <------ 1st open
if (!fp)
{
fp = fopen(filename, "w");
if (!fp)
return 0;
fputs("[]", fp);
fclose(fp);
}
// add the document to the file
fp = fopen(filename, "rb+"); // <------ Second open overwriting `fp`
//如果文件不存在,则创建该文件
FILE*fp=fopen(文件名,“r”);// 您的程序正在泄漏资源。也就是说,每次调用write_json
都会打开文件两次,同时泄漏第一次打开的描述符。经过一定数量的调用后,它已达到允许打开的文件的限制
// create file if it doesn't exist
FILE* fp = fopen(filename, "r"); // <------ 1st open
if (!fp)
{
fp = fopen(filename, "w");
if (!fp)
return 0;
fputs("[]", fp);
fclose(fp);
}
// add the document to the file
fp = fopen(filename, "rb+"); // <------ Second open overwriting `fp`
//如果文件不存在,则创建该文件
FILE*fp=fopen(文件名,“r”);//打开/关闭每个条目的文件不是一个好主意。除此之外,使用debugger查看它在迭代中执行的操作—它不写入任何内容。您为什么声称它在1020个条目后停止?我使用了,但它在124个条目后停止。打开/关闭每个条目的文件不是一个很好的主意。除此之外,请使用调试器查看它在迭代中执行的操作,它不写入任何内容。是什么让您声称它在1020个条目之后停止?我使用了一个调试器,它在124个条目之后停止。