[問題] 鏈結串列+結構指標

看板C_and_CPP (C/C++)作者 ( 興大研究生-賢)時間16年前 (2009/10/22 00:59), 編輯推噓3(309)
留言12則, 5人參與, 最新討論串1/1
各位高手大大好: 以下為小弟的程式碼,目的為將每個fin00%d.txt檔(各16筆10進位數字) 共有四個,用串列的方式,最後在一次輸出為一個48筆的out.txt檔 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <memory.h> #define size 160 void Read (char file_name[40]); //讀.txt副程式 struct node { unsigned char buffer_1D[size]; struct node *next; }; typedef struct node Node; Node *first, *current, *previous; int main(void) { int i=0,j=0,n=0; int Data_num=1; char InName[40] ="fin000.txt"; //char OutNmae[40] ="fout000.txt"; for(Data_num=1;Data_num<=4; Data_num++) { sprintf(InName,"fin00%d.txt",Data_num); //sprintf(OutNmae,"fout00%d.txt",Data_num); current = (Node*)malloc(sizeof(Node*)); Read (InName); //讀.txt檔副程式 ///----------------以下為串列部分------------- current = (Node*)malloc(sizeof(Node*)); scanf("%d",current->buffer_1D); if(Data_num==0) { first=current; } else { previous->next=current; current->next=NULL; previous=current; } } current=first; while(current!=NULL) { previous=current; current=current->next; free(previous); } //---------------------------------------- return 0; } void Read ( char file_name[40]) { int i=0,j=0,n=0; unsigned char in0; unsigned char *input; input = (unsigned char*)malloc(160*sizeof(void*)); FILE *fp_input; fp_input = fopen(file_name,"rb"); if(fp_input == NULL) { printf( "\n Can't open file %s",file_name); system("pause"); exit(0); } n=0; while ((fscanf(fp_input, "%d\n",&in0))!=EOF) { *input=in0; current=*input;//????????出現錯誤訊息 n++; } fclose(fp_input); free(input); } 小弟可以把每個.txt檔裡面的資訊一個一個讀出來, 但是沒有辦法把讀出來的資料讀進去指標結構current當中 以便利用串列把所有資料串起來, 在current=*input;會出現 error C2440: '=' : cannot convert from 'unsigned char' to 'struct node *' Conversion from integral type to pointer type requires reinterpret_cast , C-style cast or function-style cast 請各位救救我,我僵在這近四天了, 會這樣的目的是因為要應負將來資料庫不斷的存放 但是總不能每次都把程式打開來再改變一些陣列大小 所以做了這個小實驗 請各位好心人士幫幫忙,指點指點要如何改程式,或是給別的建議 我只為傳統的C而已.....XD -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.43.16.161

10/22 01:01, , 1F
current與input的型態根本不一樣呀...
10/22 01:01, 1F

10/22 01:01, , 2F
datatype 不一樣是要怎麼 assign
10/22 01:01, 2F

10/22 01:03, , 3F
strcpy(current->buffer_1D, input);
10/22 01:03, 3F

10/22 01:03, , 4F
這個可能是你要的 不過我不太確定
10/22 01:03, 4F

10/22 01:03, , 5F
你應該是想放到node的buffer裡面 而非node本身吧
10/22 01:03, 5F

10/22 01:09, , 6F
是的 我想把它放進去buffer 再串起來 行的通嗎??
10/22 01:09, 6F

10/22 10:33, , 7F
沒問題啊, 樓樓上就是在提醒你這件事, 你應該把input
10/22 10:33, 7F

10/22 10:34, , 8F
"copy"進node裡的buffer, 而不是直接assign在node上.
10/22 10:34, 8F

10/22 10:34, , 9F
事實上既然input每次都會malloc出來, struct裡的buffer
10/22 10:34, 9F

10/22 10:35, , 10F
只保留指標, 把malloc的結果存下來, 而那個free(input)
10/22 10:35, 10F

10/22 10:35, , 11F
等到程式結束前再從每個node裡對buffer做free就行了:)
10/22 10:35, 11F

10/22 16:08, , 12F
謝謝各位...我今晚會努力試試看的XD
10/22 16:08, 12F
文章代碼(AID): #1Atptr3S (C_and_CPP)
文章代碼(AID): #1Atptr3S (C_and_CPP)