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

后端 未结 8 819
逝去的感伤
逝去的感伤 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);
    
    0 讨论(0)
  • 2021-02-01 11:55

    I believe that question was about XML parsing and not about file reading, however OP should really clarify this.
    Any way you got plenty example how to read file.
    Another option to xml parsing in additional to sgm suggestion will be Expat library

    0 讨论(0)
提交回复
热议问题