[問題] 文字檔轉成數據需要養成甚麼習慣嗎?

作者: ohmylove347 (米特巴爾)   2019-04-08 15:18:33
目前是用 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;
}
作者: james732 (好人超)   2019-04-08 17:53:00
你要不要把程式碼貼上來?
作者: firejox (Tangent)   2019-04-08 19:44:00
置底有貼碼的網站
作者: ohmylove347 (米特巴爾)   2019-04-08 19:54:00
嗯嗯看到了感謝,我還在學怎麼弄
作者: idiont (supertroller)   2019-04-09 04:00:00
滿的時候reallocate成原來兩倍的大小 平均下來每次都是常數時間 可以參考C++ vector的做法fscanf的指標指的是FILE*嗎 fseek可以移動指到檔案中的位置 但是你要知道要位移多少bytes
作者: yvb   2019-04-09 13:14:00
malloc() 的參數有乘上 sizeof(), realloc() 的不用嗎?
作者: idiont (supertroller)   2019-04-09 14:35:00

Links booklink

Contact Us: admin [ a t ] ucptt.com