gpt4 book ai didi

c - 如何在C中生成1GB的xml文件

转载 作者:行者123 更新时间:2023-11-30 21:38:45 24 4
gpt4 key购买 nike

我不知道谁在 XML 标签内生成 1GB 的数据。我想在1GB大小的数据文本内生成一行数据我怎样才能以最有效(速度方面)的方式实现这一点。数据应该是可读的(当然可以是废话)

#include <stdio.h>

int main() {

printf(" --- Creating XML");

FILE *fptr;

/* open for writing */
fptr = fopen("/tmp/emp.xml", "w");

if (fptr == NULL)
{
printf("File does not exists \n");
return 1;
}


fprintf(fptr, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
fprintf(fptr, "<customer id=\"100\">\n");
fprintf(fptr, " <age>25</age>\n");
fprintf(fptr, " <name>New Customer</name>\n");
fprintf(fptr, " <data>dhgdhgdkrjgndliutghdnkljtghdliuthgdithugdnk...1GB_LONG<data>\n");
fprintf(fptr, "</customer>");

fclose(fptr);
}

最佳答案

我通过创建三个文件解决了这个问题。

与 xml 一起,直到打开数据应驻留的标记。第二个是生成的垃圾数据。第三个是 xml 其余部分的批量数据的结束标记。

像这样:

   // Open two files to be merged
FILE *fp1 = fopen("person1.xml", "r");
FILE *fp2 = fopen("randomdata.txt", "r");
FILE *fp3 = fopen("person3.xml", "r");


// Open file to store the result
FILE *fp4 = fopen("personf.xml", "w");
char c;

if (fp1 == NULL || fp2 == NULL || fp3 == NULL || fp4 == NULL)
{
puts(" --- Could not open files");
exit(0);
}

// Copy contents of first file1
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp4);

printf(" --- Done copying file 1\n");
// Copy contents of first file2
while ((c = fgetc(fp2)) != EOF)
fputc(c, fp4);

printf(" --- Done copying file 2\n");

// Copy contents of first file3
while ((c = fgetc(fp3)) != EOF)
fputc(c, fp4);

printf(" --- Done copying file 3\n");

printf("Merged files\n");

fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
return 0;

关于c - 如何在C中生成1GB的xml文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47673342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com