realloc:返回损坏的数据

时间:2017-08-09 09:10:05

标签: c realloc corrupt-data

我尝试使用C读取文件,并在使用RepaintManager.currentManager(null).setDoubleBufferingEnabled(false); 缩小大小后,我收到了损坏的数据。我真的不知道问题是什么。

这是返回字符串的函数:

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

更新

我的json文件看起来像这样:

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;
}

1 个答案:

答案 0 :(得分:2)

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

  • char car = 0;不正确:您必须将car定义为int才能更正; y区分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\\等,则需要解析字符串的完整JSON语法。
相关问题