看板 C_and_CPP 關於我們 聯絡資訊
目前是用 fscanf 和動態來寫,讀一個寫一個這樣 不過這樣變成每讀一次都要 realloc 一次感覺不太好 請問有甚麼可以讓 fscanf 的指標往回的方式嗎? 還是每讀一次就 realloc 一次就好了呢? realloc 如果原本位置不夠會給一個新的地址 把東西複製過去並且 free 掉原本的空間對吧 可是 realloc 完之後前面兩個數據都會跑掉不知道為什麼 http://i.imgur.com/7GVK0lz.jpg 抱歉問題頗多,麻煩各位了 typedef struct { int start_i, start_j; // 起點座標 int end_i, end_j; // 終點座標 int direction; // 邊的指向 int internal; // 內部方向 }coordinate; int main(int argc, char *argv[]) { FILE *fp; char str[30] = "f1.txt"; fp = fopen(str, "r"); if (NULL == fp) { printf("開檔失敗\n"); } int i = 0; int j = 0; int coordinate_i, coordinate_j; char str2[60]; coordinate *qwe = (coordinate*)malloc(10 * sizeof(coordinate)); coordinate *qwer; while (strcmp("POLYGON", str2) != 0) { fscanf(fp, "%s", &str2); printf("%s\n", str2); } i = 4; fscanf(fp, "%d %d %d %d", &(*qwe).start_i, &(*qwe).start_j, &(*qwe).end_i, &(*qwe).end_j); // 輸入邊 1. 的起點和終點座標 (*(qwe + 1)).start_i = (*qwe).end_i; (*(qwe + 1)).start_j = (*qwe).end_j; printf("邊1. (%d,%d)到(%d,%d)\n", (*qwe).start_i, (*qwe).start_j, (*qwe).end_i, (*qwe).end_j); fscanf(fp, "%d %d", &(*(qwe + 1)).end_i, &(*(qwe + 1)).end_j); (*(qwe + 2)).start_i = (*(qwe + 1)).end_i; (*(qwe + 2)).start_j = (*(qwe + 1)).end_j; printf("邊2. (%d,%d)到(%d,%d)\n", (*(qwe + 1)).start_i, (*(qwe + 1)).start_j, (*(qwe + 1)).end_i, (*(qwe + 1)).end_j); fscanf(fp, "%d %d", &(*(qwe + 2)).end_i, &(*(qwe + 2)).end_j); (*(qwe + 3)).start_i = (*(qwe + 2)).end_i; (*(qwe + 3)).start_j = (*(qwe + 2)).end_j; printf("邊3. (%d,%d)到(%d,%d)\n", (*(qwe + 2)).start_i, (*(qwe + 2)).start_j, (*(qwe + 2)).end_i, (*(qwe + 2)).end_j); fscanf(fp, "%d %d", &(*(qwe + 3)).end_i, &(*(qwe + 3)).end_j); printf("邊4. (%d,%d)到(%d,%d)\n", (*(qwe + 3)).start_i, (*(qwe + 3)).start_j, (*(qwe + 3)).end_i, (*(qwe + 3)).end_j); qwer = qwe; while ((*qwe).start_i != (*(qwe + i - 1)).end_i || (*qwe).start_j != (*(qwe + i - 1)).end_j) { i += 1; for (j = 0; j < i; j++) { printf("邊%d. (%d,%d)到(%d,%d)\n", j + 1, (*(qwe + j)).start_i, (*(qwe + j)).start_j, (*(qwe + j)).end_i, (*(qwe + j)).end_j); } printf("\n"); qwe = (coordinate *)realloc(qwe, i); // 這絕對不是好方法(每個新座標都realloc一次),之後再一起改掉(提醒一下) for (j = 0; j < i; j++) { printf("邊%d. (%d,%d)到(%d,%d)\n", j + 1, (*(qwe + j)).start_i, (*(qwe + j)).start_j, (*(qwe + j)).end_i, (*(qwe + j)).end_j); } printf("\n"); fscanf(fp, "%d %d", &(*(qwe + i - 1)).end_i, &(*(qwe + i - 1)).end_j); (*(qwe + i - 1)).start_i = (*(qwe + i - 2)).end_i; (*(qwe + i - 1)).start_j = (*(qwe + i - 2)).end_j; printf("邊%d. (%d,%d)到(%d,%d)\n\n", i , (*(qwe + i - 1)).start_i, (*(qwe + i - 1)).start_j, (*(qwe + i - 1)).end_i, (*(qwe + i - 1)).end_j); } system("pause"); return 0; } ----- Sent from JPTT on my Google Pixel 2. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.15.166.13 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1554707915.A.B6C.html
james732: 你要不要把程式碼貼上來?04/08 17:53
※ 編輯: ohmylove347 (118.168.27.209), 04/08/2019 19:43:19
firejox: 置底有貼碼的網站 04/08 19:44
※ 編輯: ohmylove347 (118.168.27.209), 04/08/2019 19:49:11
ohmylove347: 嗯嗯看到了感謝,我還在學怎麼弄 04/08 19:54
idiont: 滿的時候reallocate成原來兩倍的大小 平均下來每次都是常 04/09 04:00
idiont: 數時間 可以參考C++ vector的做法 04/09 04:00
idiont: fscanf的指標指的是FILE*嗎 fseek可以移動指到檔案中的位 04/09 04:04
idiont: 置 但是你要知道要位移多少bytes 04/09 04:04
yvb: malloc() 的參數有乘上 sizeof(), realloc() 的不用嗎? 04/09 13:14
idiont: 要 04/09 14:35