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

后端 未结 8 813
逝去的感伤
逝去的感伤 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:34

    Here is a full program that reads in a whole XML file (really, any file), into a buffer. It includes about as much error-checking as would be useful.

    N.B. everything is done in main(). Turning it into a callable function is left as an exercise for the reader.

    (Tested, compiled with GCC 4.3.3. Switches were -Wall -W --pedantic --ansi.)

    Comments on this will be addressed in approximately eight hours.

    #include 
    #include 
    
    
    int main (int argc, char *argv[]) {
     char   *buffer;        /* holds the file contents. */
     size_t  i;             /* indexing into buffer. */
     size_t  buffer_size;   /* size of the buffer. */
     char   *temp;          /* for realloc(). */
     char    c;             /* for reading from the input. */
     FILE   *input;         /* our input stream. */
    
    
     if (argc == 1) {
          fprintf(stderr, "Needs a filename argument.\n");
          exit(EXIT_FAILURE);
     }
     else if (argc > 2) {
          fprintf(stderr, "Well, you passed in a few filenames, but I'm only using %s\n", argv[1]);
     }
    
     if ((input = fopen(argv[1], "r")) == NULL) {
          fprintf(stderr, "Error opening input file %s\n", argv[1]);
          exit(EXIT_FAILURE);
     }
    
     /* Initial allocation of buffer */
     i = 0;
     buffer_size = BUFSIZ;
     if ((buffer = malloc(buffer_size)) == NULL) {
          fprintf(stderr, "Error allocating memory (before reading file).\n");
          fclose(input);
     }
    
     while ((c = fgetc(input)) != EOF) {
          /* Enlarge buffer if necessary. */
          if (i == buffer_size) {
           buffer_size += BUFSIZ;
           if ((temp = realloc(buffer, buffer_size)) == NULL) {
            fprintf(stderr, "Ran out of core while reading file.\n");
            fclose(input);
            free(buffer);
            exit(EXIT_FAILURE);
           }
           buffer = temp;
          }
    
          /* Add input char to the buffer. */
          buffer[i++] = c;
     }
    
     /* Test if loop terminated from error. */
     if (ferror(input)) {
          fprintf(stderr, "There was a file input error.\n");
          free(buffer);
          fclose(input);
          exit(EXIT_FAILURE);
     }
    
     /* Make the buffer a bona-fide string. */
     if (i == buffer_size) {
          buffer_size += 1;
          if ((temp = realloc(buffer, buffer_size)) == NULL) {
           fprintf(stderr, "Ran out of core (and only needed one more byte too ;_;).\n");
           fclose(input);
           free(buffer);
           exit(EXIT_FAILURE);
          }
          buffer = temp;
     }
     buffer[i] = '\0';
    
     puts(buffer);
    
     /* Clean up. */
     free(buffer);
     fclose(input);
    
     return 0;
    }
    

提交回复
热议问题