代码之家  ›  专栏  ›  技术社区  ›  Nabila K

realloc:返回损坏的数据

  •  1
  • Nabila K  · 技术社区  · 7 年前

    realloc 我收到损坏的数据。我真的不知道问题出在哪里。

    char *read_string(FILE *fichier) {
        char car = 0;
        size_t size = 1;
        char *symbole = realloc(NULL, sizeof(char) * size);
        char *s; 
        size_t len = 0;
        if (!symbole)
            return symbole;
        else
            s = symbole; 
        do {
            car = getc(fichier);
        } while (car != '"' && car != EOF);
        if (car == EOF)
            return EOFP; 
        else {
            car = getc(fichier);
            while (car != '"' ) {
                s[len] = car;
                car = getc(fichier);
                len++;
                if (len == size) {
                    symbole = realloc(s, sizeof(char) * (size += 1));
                    if (!symbole)
                        return symbole;
                    else
                        s = symbole; 
                }
            }
            s[len] = '\0' ;
            symbole = realloc(s, sizeof(char) * len);
            if (!symbole) {
                printf("WTF");
                return symbole;
            } else
                s = symbole;  
            return s;
        }
    }
    

    main

    int main(int argc, char *argv[]) {
    
        FILE *fichier = NULL;
        fichier = fopen("C:/Users/Nabila K/Documents/test.json", "r");
    
        if ((fichier != NULL)) {
            while (feof(fichier) == 0) {
                char *test = read_string(fichier);
                if (test == NULL) {
                    printf("test == NULL\n");
                    exit(1);
                } else
                if (test == EOFP) {
                } else {
                    printf("%s\n", test);
                    free(test);
                } 
            } 
            fclose(fichier);   
        } else {
            exit(EXIT_FAILURE);
        }
        return 0;
    }
    

    我的json文件如下所示:

    {
        "KARIM BENNI" : {
           "2017-08-07 09:50:50" : {
              "Anomalie" : {
                "description" : "Test",
                "theme" : "Engins mobiles"
              },
              "date" : "2017-08-07",
              "date_now" : "2017-08-07 09:50:50",
              "entite" : "USINE LAMINAGE A FROID",
              "etat" : "Cree",
              "nb_personne" : 2,
              "temps" : 5,
              "visiteur" : "KARIM BENNI",
              "visite" : "AHMED RABII",
              "zone" : "COUPE"
           }
        }
        }
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   chqrlie    4 年前

    您的代码中存在多个问题:

    • char car = 0; 不正确:必须定义 car int 要正确区分返回的所有值 getc() EOF .

    • while (feof(fichier) == 0) Why is “while ( !feof (file) )” always wrong?

    • EOFP NULL 相反,为了更清晰。

    • 决赛 realloc() 分配的块太短一个字节。你必须保持 len+1 字节 len 角色 空终止符。

    以下是一个简化和更正的版本:

    #include <stdio.h>
    #include <stdlib.h>
    
    char EOFP[1];   /* special value used to signal end of file */
    
    char *read_string(FILE *file) {
        int c;
        size_t size, len;
        char *symbol;
        char *s;
    
        while ((c = getc(file)) != '"') {
            if (c == EOF)
                return EOFP;
        }
        size = 16;
        len = 0;
        symbol = malloc(size);
        if (symbol == NULL) {
            /* allocation failure */
            return NULL;
        }
        while ((c = getc(file)) != '"') {
            if (c == EOF) {
                /* premature end of file in the middle of a string */
                free(symbol);
                return EOFP;
            }
            if (len + 2 < size) {
                size += size;
                s = realloc(symbol, size);
                if (s == NULL) {
                    /* allocation failure */
                    free(symbol);
                    return NULL;
                }
                symbol = s;
            }
            symbol[len++] = c;
        }
        symbol[len] = '\0';
        s = realloc(symbol, len + 1);
        return s ? s : symbol;
    }
    
    int main(int argc, char *argv[]) {
    
        FILE *file = fopen("C:/Users/Nabila K/Documents/test.json", "r");
        if (file != NULL)) {
            char *test;
            while ((test = read_string(file)) != EOFP) {
                if (test == NULL) {
                    printf("test == NULL\n");
                    exit(1);
                } else {
                    printf("%s\n", test);
                    free(test);
                }
            } 
            fclose(file);   
        } else {
            exit(EXIT_FAILURE);
        }
        return 0;
    }
    

    • 如果字符串可以包含转义字符,例如 \" \n , \\
    推荐文章