c语言读取文件链表数据错误,把链表数据存到文件再从文件读出数据放到链表 怎么出错了...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

printf("请输入数学:\n");

scanf("%d",&p1->info.ke2);

printf("请输入英语:\n");

scanf("%d",&p1->info.ke3);

}

p2->next=NULL;

if((fp=fopen("C:\\Users\\ybh\\Desktop\\stu.txt","w"))==NULL) {

printf("error!!!!!\n");

exit(0);

}

p=head;

if (head!=NULL)

do

{

if(fprintf(fp,"%ld%s%c%d%d%d",p->info.xuehao,p->info.xingming,p->info.xingbie,p->info.ke1,p->info.ke2,p->info.ke3)!=1) printf("error!\n");

p=p->next;

}while(p!=NULL);

fclose(fp);

return 0;

}

以下是从文件中读取刚才放入的 数据 放到链表中

#include "stdafx.h" #include "stdio.h" #include "stdlib.h"

#define LEN sizeof(struct Student)

struct StudentDate

{

long xuehao;

char xingming[20];

char xingbie;

int ke1;

int ke2;

int ke3;

};

struct Student

{

struct StudentDate info;

struct Student *next;

};

int main(int argc, char* argv[])

{

int n=0;

char j;

struct Student *head,*p1,*p2,*p;

FILE*fp;

if((fp=fopen("C:\\Users\\ybh\\Desktop\\stu.txt","r"))==NULL)

{

printf("error!\n");

exit(0);

}

head=NULL;

p1=p2=(struct Student*) malloc (LEN); fscanf(fp,"%ld%s%c%d%d%d",&p1->info.xuehao,&p1->info.xingming,&p1->info.xingbie,&p1->info.ke1,&p1->info.ke2,&p1->info.ke3);

while (p1->info.ke1!=0)

{ //这里应该形成死循环了 不知道为什么

n++;

if(n==1) head=p1;

else p2->next=p1;

p2=p1;

p1=(struct Student*)malloc(LEN);

fscanf(fp,"%ld%s%c%d%d%d",&p1->info.xuehao,&p1->info.xingming,&p1->info.xingbie,&p1->info.ke1,&p1->info.ke2,&p1->info.ke3);

}

p2->next=NULL;

fclose(fp);

p=head;

printf("读入完成,是否输出到屏幕?(y/n)\n");

scanf("\n");

scanf("%c",&j);

if (j=='y'||j=='Y')

{

do

{

printf("学号:%ld 姓名:%s 性别:%c 语文:%d 数学:%d 英语:%d\n",p->info.xuehao,p->info.xingming,p->info.xingbie,p->info.ke1,p->info.ke2,p->info.ke3);

p=p->next;

}while(p!=NULL);

}

return 0;

}

以上应该是2个相反的过程 写入数据和读出数据 但是可以建立文件 而从文件中无法读取数据 程序可以 运行 但是在标记的那里形成了死循环 请问为什么 如何 解决

找了2天了 找不出问题 第一次 发帖 求帮忙啊

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现读取链表数据并存储在文件fee.dat中,可以按照以下步骤进行: 1. 创建链表结构体和节点结构体,如下所示: ```c struct node { int data; struct node* next; }; struct list { struct node* head; struct node* tail; }; ``` 2. 定义文件指针,并打开文件: ```c FILE* fp; fp = fopen("fee.dat", "rb"); ``` 3. 创建一个空链表: ```c struct list mylist; mylist.head = NULL; mylist.tail = NULL; ``` 4. 读取文件中的数据,并将数据存储在链表中: ```c while (fread(&data, sizeof(int), 1, fp) == 1) { struct node* new_node = (struct node*)malloc(sizeof(struct node)); new_node->data = data; new_node->next = NULL; if (mylist.head == NULL) { mylist.head = new_node; mylist.tail = new_node; } else { mylist.tail->next = new_node; mylist.tail = new_node; } } ``` 5. 关闭文件: ```c fclose(fp); ``` 完整代码如下所示: ```c #include <stdio.h> #include <stdlib.h> struct node { int data; struct node* next; }; struct list { struct node* head; struct node* tail; }; int main() { FILE* fp; fp = fopen("fee.dat", "rb"); if (fp == NULL) { printf("Failed to open file.\n"); return 0; } struct list mylist; mylist.head = NULL; mylist.tail = NULL; int data; while (fread(&data, sizeof(int), 1, fp) == 1) { struct node* new_node = (struct node*)malloc(sizeof(struct node)); new_node->data = data; new_node->next = NULL; if (mylist.head == NULL) { mylist.head = new_node; mylist.tail = new_node; } else { mylist.tail->next = new_node; mylist.tail = new_node; } } fclose(fp); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值