磊磊映画 发表于 2014-5-18 13:06:31

C语言—文件数据读入到链表里不能实现

1.正在做学生管理系统,深刻学习C语言
2.已经可以生成链表(见creat函数),并把生成的链表保存到student.txt文件(见store函数).
3.并且可以打印出生成的链表(见print函数)。
4.不能实现:将student.txt文件的数据读入链表中 (见load函数),求大神指导!
环境VC++ 6.0
上代码:
#include <stdio.h>
#include <stdlib.h>
#include "malloc.h"
#include "process.h"
#define LEN sizeof(struct student)

int n;

struct student
{
   long num;
   float score;
   struct student *next;
};









struct student *creat(void)
{
   struct student *head;
   struct student *p1,*p2;
   n=0;
   p1=p2=(struct student *)malloc(LEN);
   scanf("%ld,%f",&p1->num,&p1->score);
   head=NULL;
   while(p1->num!=0)
   {
   n=n+1;
       if(n==1)head=p1;//把首地址给链表头
   else p2->next=p1;//指针下移
       p2=p1;//更新p2指针
       p1=(struct student *)malloc(LEN);//申请新的地址&更新p1指针
       scanf("%ld,%f",&p1->num,&p1->score);

   }
   p2->next=NULL;
       return(head);



}



struct student *print(struct student *head)
{
    struct student *p;

        p=head;
        printf("链表中共用%d条记录\n",n);
        if(head!=NULL)
        {
      
           do
           {
                   printf("%ld,%5.1f\n",p->num,p->score);
                   p=p->next;
           }
       while(p!=NULL);

        }
   return(head);
}


/*
   function:将生成结构体中的数据保存到文件中
   time:2014-5-16 9:42:49
   
   version:V1.0
   input:要存入链表的首地址
   output:void
   链表默认保存到student.txt文件中
*/
void store(struct student *head)
{
    FILE *fp;
        struct student *p;
        fp=fopen("student.txt","w+");//新建文件
        if(fp==NULL)
        {
      printf("open file failed");
          exit(0);
        }
        p=head;
    if(head!=NULL)
        {
                do
                {
          fprintf(fp,"%ld,%f",p->num,p->score);
                  p=p->next;
                }
      while(p!=NULL);
        }
        fprintf(fp,"0");
        fclose(fp);//关闭文件
        printf("Linked list store is ok!\n");
       
}


/*
function:将文件中的数据读入到链表中

time:2014-5-16 10:12:23
version:V1.0
input:void
ouput:返回链表的头指针
读取默认文件 student.txt
*/
struct student *load(void)
{
   FILE *fp;
   struct student *head;
   struct student *p1,*p2;
   fp=fopen("student.txt","r");//打开文件
   rewind(fp);
   if(fp==NULL)
        {
      printf("open file failed");
          exit(0);
        }

   n=0;
   head=NULL;
   p1=p2=(struct student *)malloc(LEN);
   fscanf(fp,"%ld,%f",&p1->num,&p1->score);
   while(p1->num!=0)
   {
      n=n+1;
      if(n==1)head=p1;
          else p2->next=p1;
          p2=p1;
          p1=(struct student *)malloc(LEN);

          fscanf(fp,"%ld,%f",&p1->num,&p1->score);
   }
    p2->next=NULL;
        fclose(fp);//关闭文件
        return(head);

}

albert_w 发表于 2014-5-18 14:18:55

可能遇到新行问题了, fscanf格式化串第一个字符前加空格试试看.
程序很粗暴啊

磊磊映画 发表于 2014-5-18 14:23:25

albert_w 发表于 2014-5-18 14:18
可能遇到新行问题了, fscanf格式化串第一个字符前加空格试试看.
程序很粗暴啊 ...

高手的ID很熟啊, 我试试

磊磊映画 发表于 2014-5-18 14:24:41

磊磊映画 发表于 2014-5-18 14:23
高手的ID很熟啊, 我试试

不行,停止运行了

albert_w 发表于 2014-5-18 14:32:12

那就用fgets一行一行取出来玩儿吧...
还在哪里见这id?{:lol:}

磊磊映画 发表于 2014-5-18 15:01:26

顶一下,调试不能通过fputcfread 用过也

mhw 发表于 2014-5-18 16:48:48

告诉你个大招:单步调试

3050311118 发表于 2014-5-18 17:30:25

都用电脑做程序了还用链表啊,直接数据库啊,小型的sqlite也可以

wangpengcheng 发表于 2014-5-18 21:15:26

mhw 发表于 2014-5-18 16:48
告诉你个大招:单步调试

我也有个大招:设断点!

磊磊映画 发表于 2014-5-19 16:18:04

3050311118 发表于 2014-5-18 17:30
都用电脑做程序了还用链表啊,直接数据库啊,小型的sqlite也可以

小弟是搞单片机的 学C语言是给单片机用,所以学链表了

磊磊映画 发表于 2014-5-19 16:18:38

wangpengcheng 发表于 2014-5-18 21:15
我也有个大招:设断点!

好的明白了 VC6.0++跟MDK差不多

wangpengcheng 发表于 2014-5-19 18:12:46

磊磊映画 发表于 2014-5-19 16:18
好的明白了 VC6.0++跟MDK差不多

调试的方法都差不多,呵呵!其实我发现RTT里面的链表很好用的,好像在一个K什么的.h文件中,你找找!

takashiki 发表于 2014-5-19 20:25:37

fprintf、fscanf第二个参数要加上\n。比如这样:fprintf(fp,"%ld,%f\n",p->num,p->score);   fscanf(fp,"%ld,%f\n",&p1->num,&p1->score);

3050311118 发表于 2014-5-19 20:46:56

磊磊映画 发表于 2014-5-19 16:18
小弟是搞单片机的 学C语言是给单片机用,所以学链表了

SQLITE比较简单的你自个可以百度下查查   与其花这么大力气搞链表 啥的存储数据   还不如用那个 以后记录查询等操作相当省事
我也是搞单片机开始的    现在搞的偏上了 有好用的东西可以用你干么费那么大力气呢

磊磊映画 发表于 2014-5-20 08:01:00

takashiki 发表于 2014-5-19 20:25
fprintf、fscanf第二个参数要加上\n。比如这样:fprintf(fp,"%ld,%f\n",p->num,p->score);   fscanf(fp," ...

大神级别的回复 谢谢

qlb1234 发表于 2014-5-20 09:24:03

create
   

磊磊映画 发表于 2014-5-21 17:23:19

qlb1234 发表于 2014-5-20 09:24
create

什么意思?

qlb1234 发表于 2014-5-21 17:23:58

磊磊映画 发表于 2014-5-21 17:23
什么意思?

拼錯。

磊磊映画 发表于 2014-5-21 21:10:02

qlb1234 发表于 2014-5-21 17:23
拼錯。

还是不懂

qlb1234 发表于 2014-5-21 21:38:43

磊磊映画 发表于 2014-5-21 21:10
还是不懂

沒事了。打擾。

磊磊映画 发表于 2014-5-22 16:34:27

qlb1234 发表于 2014-5-21 21:38
沒事了。打擾。

同样感谢你

磊磊映画 发表于 2014-5-23 17:05:23

本帖最后由 磊磊映画 于 2014-5-23 17:07 编辑

我是来结贴的。
1.在百度找不到答案,百度上的代码说是链表,但大部分是结构体数组2.论坛提问没有针对性解答的情况下3.我决定自己来,经过不断地调试,纠结几天之后,终于解决了问题。
问题的原因是:
1.链表之前存入到文件中的每一字节数据没有标记符号,所以之后读取时就找不到数据了没有开始和结束标记
2.把文件的数据读入到链表中,是通过判断是否是文件结尾结束链表增长的。feof()函数
3.以前数据是按如下格式写入文本中的,data代表数据
data1,data2data3,data4data5,data6.........
现在数据是按这种格式写入到文本中的
data1,data2,data3,data4,data5,data6........
通过对比就知道以前在程序中根本就区别不出“data2跟data3”位置上的数据。
按照新的格式写入和读取就成功了。
贴上最新成功的代码:
/*
   function:将生成结构体中的数据保存到文件中
   time:2014-5-16 9:42:49
   author:zhanglei
   version:V1.0
   input:要存入链表的首地址
   output:void
   链表默认保存到student.txt文件中
*/
void store(struct student *head)
{
    FILE *fp;
        struct student *p;
        fp=fopen("student.txt","w+");//新建文件
        if(fp==NULL)
        {
      printf("open file failed");
          exit(0);
        }
        p=head;
    if(head!=NULL)
        {
                do
                {
          fprintf(fp,"num is %ld,score is %f,",p->num,p->score);
                  p=p->next;
                }
      while(p!=NULL);
        }

        fclose(fp);//关闭文件
        printf("Linked list store is ok!\n");
       
}


/*
function:将文件中的数据读入到链表中
author:zhanglei
time:2014-5-16 10:12:23
version:V1.0
input:void
ouput:返回链表的头指针
读取默认文件 student.txt
*/
struct student *load(void)
{
   FILE *fp;
   struct student *head;
   struct student *p1,*p2;
   p1=p2=NULL;
   n=0;
   p1=p2=(struct student *)malloc(LEN);//ERROR
   fp=fopen("student.txt","r+");//打开文件
   rewind(fp);
   if(fp==NULL)
        {
      printf("open file failed");
          exit(0);
        }

   
   fscanf(fp,"num is %ld,score is %f,",&p1->num,&p1->score);
   head=NULL;
   while(!feof(fp))
   {
      n=n+1;
      if(n==1)head=p1;
          else p2->next=p1;
          p2=p1;
          p1=(struct student *)malloc(LEN);

          fscanf(fp,"num is %ld,score is %f,",&p1->num,&p1->score);
   }
    p2->next=NULL;
        fclose(fp);//关闭文件
        return(head);

}
页: [1]
查看完整版本: C语言—文件数据读入到链表里不能实现