How can I read an XML file into a buffer in C?

后端 未结 8 816
逝去的感伤
逝去的感伤 2021-02-01 11:03

I want to read an XML file into a char *buffer using C.

What is the best way to do this?

How should I get started?

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 11:55

    You can use the stat() function to get the file size. then allocate a buffer using malloc after it reading the file using fread.

    the code will be something like that:

    struct stat file_status;
    char *buf = NULL;
    FILE * pFile;
    
    stat("tmp.xml", &file_status);
    buf = (char*)malloc(file_status.st_size);
    pFile = fopen ("tmp.xml","r");
    fread (buf,1,file_status.st_size,pFile);
    
    fclose(pFile);
    

提交回复
热议问题