[問題] 將動態記憶體裡的值輸出成一.txt

看板C_and_CPP (C/C++)作者 ( 興大研究生-賢)時間16年前 (2009/10/22 19:23), 編輯推噓1(101)
留言2則, 2人參與, 最新討論串1/1
各位大大..抱歉打擾了 我把4個fin00%d.txt檔的資料鏈結串列起來成ㄧ個out.txt 可是輸出都是只輸出最後一個.txt裡頭的資訊 以下是我的程式碼 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <memory.h> #define size 160 void Read (char file_name[40]); void write (); void append(); int Data_num; struct node { unsigned char buffer_1D[size]; struct node *next; }; typedef struct node Node; Node *first, *current; unsigned char *output; unsigned char buffer[4*size]={0}; int main(void) { int i=0,j=0,n=0; output = (unsigned char *)malloc(sizeof(void*)); char InName[40] ="fin000.txt"; for(Data_num=1;Data_num<=4; Data_num++) { sprintf(InName,"fin00%d.txt",Data_num); first = (Node*)malloc(sizeof(Node*)); Read (InName); first->next=NULL; current=first; printf(" %d\n",current->buffer_1D[n]); system("pause"); append(); while(first!=NULL) { first=first->next; } write(); } return 0; } void append() { Node *previous; previous=(Node*)malloc(sizeof(Node*)); current->next=previous; previous->next=NULL; current=previous; } void Read ( char file_name[40]) { int i=0,j=0,n=0; unsigned char in0; 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)// && first!=NULL) { first->buffer_1D[n]=in0; buffer[n]=in0; n++; //printf("%d %d %d\n",n,first->buffer_1D[n] ,in0); //system("pause"); if(n>size) { break; } } fclose(fp_input); } void write() { FILE *fp_output; int i=0,j=0,n=0; fp_output =fopen("out.txt","wb"); if (fp_output == NULL) { printf("\n Can't open file \n"); } else { for(n=0; n<size ; n++)//這裡是只會輸出最後ㄧ個.txt裡頭資訊的原因 { *output=buffer[n]; fprintf(fp_output," %d \n",*output); } } fclose(fp_output); } 可否指點一下怎嚜把四個.txt的資訊循序輸入經串列一起輸出成ㄧ個.txt檔 因為我不知道怎嚜觀看動態記憶體裡頭的資訊 所以可否請各位教學一下..拜託拜託!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.43.9.248

10/22 23:48, , 1F
malloc(sizeof(Node*)) 應該改成 Node 吧
10/22 23:48, 1F

10/23 09:32, , 2F
fopen(xxx, "wb+"); 才是append
10/23 09:32, 2F
文章代碼(AID): #1Au42TKr (C_and_CPP)
文章代碼(AID): #1Au42TKr (C_and_CPP)