[問題] 讀檔填入陣列後想印出陣列內容
問題(Question):
我從一個文字檔讀取內容string
將內容填入二維陣列, 並印出此二維陣列的內容
發現新的string會蓋掉所有已存的string
餵入的資料(Input):
文字檔內容:
OOO XXX
預期的正確結果(Expected Output):
印出:
OOO XXX
錯誤結果(Wrong Output):
印出:
XXX XXX
程式碼(Code):(請善用置底文網頁, 記得排版)
1 unsigned char *my_table[1][2] = {{0}, {0}};
2 FILE *fptr = fopen(filepath, "r");
3 char str[10] = {0};
4 int i=0;
6 fscanf(fptr, "%s", str);
7 my_table[0][0] = str;
8 printf("%s\t", my_table[0][0]); //印出OOO ok
10 fscanf(fptr, "%s", str);
11 my_table[0][1] = str;
12 printf("%s\n", my_table[0][1]); //印出XXX 也ok
14 printf("%s\t", my_table[0][0]); //印出XXX <--不懂
15 printf("%s\n", my_table[0][1]);
return 0;
補充說明(Supplement):
因為第8行跟第12行都正確, 但是第14行錯了
因此判斷問題是出在10, 11行那..可是不懂是怎樣的錯誤?
另外若賦值是直接給字串, 例如
my_table[0][0] = "OOO";
my_table[0][1] = "XXX";
這樣最後印出來也正確~ 就是讀檔來賦值會有誤
麻煩指點一下迷津, 謝謝!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.120.186.181
※ 編輯: Crysis 來自: 59.120.186.181 (07/02 17:21)
→
07/02 17:24, , 1F
07/02 17:24, 1F
→
07/02 17:26, , 2F
07/02 17:26, 2F
→
07/02 17:27, , 3F
07/02 17:27, 3F
→
07/02 17:29, , 4F
07/02 17:29, 4F
→
07/02 17:30, , 5F
07/02 17:30, 5F
→
07/02 17:31, , 6F
07/02 17:31, 6F