作者hsien0701 ( 兴大研究生-贤)
看板C_and_CPP
标题[问题] 链结串列+结构指标
时间Thu Oct 22 00:59:30 2009
各位高手大大好:
以下为小弟的程式码,目的为将每个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
1F:→ james732:current与input的型态根本不一样呀... 10/22 01:01
2F:推 ducksteven:datatype 不一样是要怎麽 assign 10/22 01:01
3F:→ james732:strcpy(current->buffer_1D, input); 10/22 01:03
4F:→ james732:这个可能是你要的 不过我不太确定 10/22 01:03
5F:推 nowar100:你应该是想放到node的buffer里面 而非node本身吧 10/22 01:03
6F:→ hsien0701:是的 我想把它放进去buffer 再串起来 行的通吗?? 10/22 01:09
7F:推 VictorTom:没问题啊, 楼楼上就是在提醒你这件事, 你应该把input 10/22 10:33
8F:→ VictorTom:"copy"进node里的buffer, 而不是直接assign在node上. 10/22 10:34
9F:→ VictorTom:事实上既然input每次都会malloc出来, struct里的buffer 10/22 10:34
10F:→ VictorTom:只保留指标, 把malloc的结果存下来, 而那个free(input) 10/22 10:35
11F:→ VictorTom:等到程式结束前再从每个node里对buffer做free就行了:) 10/22 10:35
12F:→ hsien0701:谢谢各位...我今晚会努力试试看的XD 10/22 16:08