我想問一下爲什麼這段代碼導致了分段錯誤。我試圖從文本文件中獲取輸入,但我無法弄清楚問題所在。文件訪問中的分段錯誤
using namespace std;
using namespace cv;
int main()
{
char str[50];
FILE *trainfile;
int k, n, maxval1, maxval2, classnum;
char dataArray[n][3];
trainfile = fopen("training.txt", "r+");
if(trainfile == NULL){
perror("Cannot open file.\n");
}else{
while(!feof(trainfile)){
fscanf(trainfile, "%s", str);
}
}
fclose(trainfile);
return 0;
}
確定50個字符就夠了?另外,如果trainfile == NULL,那麼你調用fclose(NULL) – slezica 2012-01-14 14:37:04