读取文件数据并赋予链表
我想把txt文件中的数据附到链表当中,以便进行编辑运算,可是赋值那块总有问题,实在是不知道该怎么去赋,运行总是中断
程序代码:#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
int no;
long num;
char *name;
char *x_b;
float score;
struct student *next;
};
int n;
void main()
{
FILE *fp;
struct student *head;
struct student *p1;
struct student *p2;
char ch;
n=0;
p1=(struct student*)calloc(52,LEN);
if((fp=fopen("1.txt","r"))==NULL)
{
printf("不能打开文件1.txt");
exit(0);
}
ch=fgetc(fp);
while(ch!='\n') /*输出第一行,第一行为表头,单独输出来,让指针指向第一个数据*/
{
putchar(ch);
ch=fgetc(fp);
}
putchar('\n');
while(!feof(fp)) /*给链表赋值。。。就这有问题。。。*/
{
fscanf(fp,"%d%ld%s%s%f",&p1->no,&p1->num,p1->name,p1->x_b,&p1->score);
n=n+1;
if(n==1)
head=p1;
else p2->next=p1;
p2=p1;
p1=p1->next;
}
fclose(fp);
}[ 本帖最后由 核晴核理 于 2012-9-13 08:58 编辑 ]









