在我的程序中,我從.txt文件接收文本。下面是一行文字的例子:realloc的問題 - 「無效的下一個大小」
12X15 DE3 DS4 dn9 g2,7 m5,9 m3,1 h2,2
我想使用的strtok()分手的每塊文本;然後我將把每個塊作爲一個元素放入一個數組中。所以一串字符串。
這是到目前爲止我的代碼:
void parseFile(char ** argv) {
FILE *textFile;
char *string;
char **lineToken;
int i;
textFile = fopen(argv[1], "r");
lineToken = malloc(sizeof(1));
string = malloc(sizeof(MAX_CHAR));
while(fgets(string, MAX_CHAR, textFile) != NULL) { /* Get first line of text */
lineToken[0] = strtok(string, " "); /* Put first element in lineToken[0] */
for(i = 1; i; i++) {
/* Realloc because total number of elements is unknown */
lineToken = realloc(lineToken, i + 1);
/* Put new strtok'd string into lineToken[i] */
lineToken[i] = strtok(NULL, " ");
}
for(i = 0; i; i++) {
move(i, 0);
printw("%s", lineToken[i]);
refresh();
}
}
free(lineToken);
free(string);
} /* End of function */
但我不斷收到這樣的realloc錯誤:
*** glibc detected *** ./bin/a3RunMe: realloc(): invalid next size: 0x01f2a270 ***
Aborted
當然你的意思是'printf'而不是'printw'。 – ajay
我的歉意。我正在使用ncurses庫,因此在屏幕上打印文本需要printw。 – Plaidypus