所以我必須讓這個程式將一個巨大的 .txt 檔案讀入 AVL,為此,我需要讀取文本檔案中的所有格式化資料并將其放入 AVL。但是,每當我嘗試在我的代碼(空指標)中初始化 AVL 時,一旦它到達我用來從 .txt 檔案中收集字串的 fscanf 函式,它就會破壞代碼。我就在這里制作了這個演示,我認為我非常接近問題的根源。我將范圍縮小到與在 fscanf 函式之前用 NULL 值初始化指標有關。但是我該如何解決這個問題?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
FILE * filePointer = fopen("_lexico_shuf.txt", "r");
if(!filePointer) {
printf("can't open the file");
exit(101);
}
char *lexiconWord;
float polarity;
int *a = NULL;
printf("before while");
while (!feof(filePointer)) {
fscanf(filePointer, "%[^;];%f\n", lexiconWord, &polarity);
printf("| (%s) (%.1f) |", lexiconWord, polarity);
}
printf("after while");
}
所以唯一列印在螢屏上的是“before while”printf,而不是“after while”。并且程式回傳一個亂數。
uj5u.com熱心網友回復:
lexiconWord尚未設定為指向任何地方,因此fscanf使用無效的指標值嘗試寫入。
將此變數更改為陣列,并在fscanf不超出緩沖區的情況下使用欄位寬度,并檢查fscanf.
char lexiconWord[100];
...
int rval = fscanf(filePointer, "
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/360987.html
