gpt4 book ai didi

c - 重新分配 : corrupted data returned

转载 作者:太空宇宙 更新时间:2023-11-04 02:29:31 24 4
gpt4 key购买 nike

我正在尝试使用 C 读取文件,在使用 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;
}
}

我的主要函数是:

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

最佳答案

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

  • char car = 0;不正确:您必须定义 car作为int正确区分 getc() 返回的所有值, 特别是 EOF .

  • while (feof(fichier) == 0)总是错的。了解原因:Why is “while ( !feof (file) )” always wrong?

  • EOFP未定义,您可能应该使用 NULL而是为了更清楚。

  • 最后 realloc() shrink 分配的 block 太短了一个字节。你必须保留 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;
}

注意事项:

  • 如果字符串可以包含转义字符(例如 \"),则需要解析字符串的完整 JSON 语法。或 \n , \\

关于c - 重新分配 : corrupted data returned,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45586293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com