[問題] Queue讀入資料

看板C_and_CPP作者 (Eton)時間10年前 (2013/11/13 13:27), 編輯推噓2(205)
留言7則, 4人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 想請教一下為什麼我show出來的結果皆為0 餵入的資料(Input): 預期的正確結果(Expected Output): 將materials.txt的資料讀進QUEUE 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) int _tmain(int argc, _TCHAR* argv[]) { FILE* f1; fopen_s(&f1,"materials.txt","r"); char x; createq(); while(fscanf_s(f1,"%c",&x)!=EOF) { addq(x); } fclose(f1); showqueue(); system("pause"); return 0; } void createq(){ rear=front=(Node*)malloc(sizeof(Node)); front->next=rear->next=NULL; } void addq(char x){ Node* newnode; newnode = (Node*) malloc(sizeof(Node)); if(front->next == NULL) front->next = newnode; newnode->data = x; newnode->next = NULL; rear->next = newnode; rear = newnode; } void showqueue() { Node* tmpnode; tmpnode = front->next; printf("\n佇列內容:"); while(tmpnode != NULL) { printf("%d ", tmpnode->data); tmpnode = tmpnode->next; } } 補充說明(Supplement): 麻煩各位大大指點迷津了,感恩! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.114.212.113

11/13 15:14, , 1F
讀進?讀入?你到底是要讀還寫啊
11/13 15:14, 1F

11/13 17:35, , 2F
沒check error,先猜開檔失敗,再猜txt內容格式不符
11/13 17:35, 2F

11/14 19:53, , 3F
fscanf_s(f1,"%c",&x) 應改為 fscanf_s(f1,"%c",&x, 1) ??
11/14 19:53, 3F

11/15 17:52, , 4F
fscanf用法:fscanf(fp,"%d",&var)
11/15 17:52, 4F

11/15 17:53, , 5F
fscanf_s用法:fscanf(fp,"%d",&var,sizeof(int))
11/15 17:53, 5F

11/15 17:53, , 6F
区别:fscanf_s需要指定长度
11/15 17:53, 6F

11/15 17:54, , 7F
GOOGLE的
11/15 17:54, 7F
文章代碼(AID): #1IWmsmaN (C_and_CPP)