社区
C++ 语言
帖子详情
文件内容存入链表
xiaoge2030
2015-10-17 10:45:09
我想把如图所示的txt文件内容存到链表中去,例如201570494 张三 男 90 89 78,依次是学号,姓名,性别,成绩(不同的元组,成绩个数可能不同)。自己用vc++编了个程序,调试时只能读到第一行元组;运行程序并得不到输出。我自己觉得这里面应该要用到链表的嵌套吧,但程序出错检查不出来。请各位大神指导!
...全文
148
3
打赏
收藏
文件内容存入链表
我想把如图所示的txt文件内容存到链表中去,例如201570494 张三 男 90 89 78,依次是学号,姓名,性别,成绩(不同的元组,成绩个数可能不同)。自己用vc++编了个程序,调试时只能读到第一行元组;运行程序并得不到输出。我自己觉得这里面应该要用到链表的嵌套吧,但程序出错检查不出来。请各位大神指导!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
xiaoge2030
2015-10-18
打赏
举报
回复
已解决!!!
xiaoge2030
2015-10-17
打赏
举报
回复
while((ifile>>number>>name>>sex)&&(score_head=score_list()))调试的时候在这个地方有问题。 自己编的代码如上,初学很多地方写的不规范,还请大神们指导!
xiaoge2030
2015-10-17
打赏
举报
回复
#include <iostream> #include<string> #include <fstream> using namespace std; ifstream ifile("成绩.txt",ios_base::in); struct score //学生成绩结构体 { int score_num; score *score_next; }; struct student //学生信息的结构体 { int Sid; string Name; string Sex; score data; /*int data;*/ student *next; }; void score_ShowList(student *L); void ShowList(student *L); score* score_list(); student* list(); int main() { student* head; head=list(); cout<<"文件内容存入离链表结束。"<<endl; cout<<"文件内容:"<<endl; ShowList(head); return 0; } score* score_list()//分数存入分数链表 { int score_num; int i=1; score *score_head=NULL; score_head=new score; score *score_p0,*score_p1;//p0指向最后的元素,p1是临时存储地址 while ((ifile>>score_num)&&(score_num<=100)) { if (i==1) { score_head->score_next=new score; score_head->score_next->score_num=score_num; score_head->score_next->score_next=NULL; score_p1=score_head->score_next;//p0指向最后的元素,p1是临时存储地址 } else { score_p0=new score; score_p0->score_num=score_num; score_p0->score_next=NULL; score_p1->score_next=score_p0; score_p1=score_p0; } i++; } return score_head; } student* list()//学生信息存入链表 { student *head=NULL; student *p0,*p1;//q指向头指针,p开辟student的单元 int i=1; /*ifstream ifile("成绩.txt",ios_base::in);*/ if(ifile) { while(!ifile.eof()) { string name,sex; int number; /*int data;*/ score *score_head; while((ifile>>number>>name>>sex)&&(score_head=score_list())) /*while(ifile>>number>>name>>sex>>data)*/ { if(i==1) { head=new student; head->Sid=number; head->Name=name; head->Sex=sex; head->data=*score_head; /*head->data=data;*/ head->next=NULL; p1=head;//p0指向最后的元素,p1是临时存储地址 } else { p0=new student; p0->Sid=number; p0->Name=name; p0->Sex=sex; p0->data=*score_head; /*p0->data=data;*/ p0->next=NULL; p1->next=p0; p1=p0; } i++; } } ifile.close(); } cout<<"取出成功!"<<endl; return head; //返回头节点 } void ShowList(student *L)//输出函数 { student *p; p=L; if(p==NULL) { cout<<"空链表!"<<endl; } else { while (p!=NULL) { cout<<"学号:"<<p->Sid<<"\t"<<"姓名:"<<p->Name<<"\t"<<"性别:"<<p->Sex<<"\t"<<"成绩:"; score_ShowList(p); /*cout<<"学号:"<<p->Sid<<"\t"<<"姓名:"<<p->Name<<"\t"<<"性别:"<<p->Sex<<"\t"<<"成绩:"<<p->data; p=p->next;*/ cout<<endl; } } cout<<"链表内容输出完毕:"; } void score_ShowList(student *L)//分数链表的输出函数 { score a; a=L->data; score *b; b=a.score_next; if(b==NULL) cout<<"没有输入分数!"<<endl; else { while (b!=NULL) { cout<<b->score_num<<"\t"; b=b->score_next; } } cout<<endl; }
将
文件
内容
存入
链表
~
#include #include #include#include using namespace std; main(....){vector vec1; ifstream record1; record1.close();record1.clear();record1.open( filename ); if( !rec
c语言读取txt
存入
链表
,各位大神能否说下C语言如何在程序开始时候将
文件
内容
读取到
链表
中呢...
#include#includetypedefstructstudent{intid;charname[32];structstudent*next;}ST;ST*create(ST*h)//头插创建
链表
;{ST*head=h;ST*p=(ST*)malloc(sizeof(ST));printf("请输入学号、姓名\n");scanf("%d%s",&p-...
c语言
文件
读取字符串到
链表
,请教C语言怎么把
链表
存入
文件
,从
文件
读取到
链表
...
请问C语言如何把
链表
存入
文件
,从
文件
读取到
链表
Consumer*read_list(){FILE*fp;if((fp=fopen("CONSUMER.dat","rb"))==NULL){printf("无法读取CONSUMER.dat\n");returnNULL;}intsign;Consumer*s,*p,*head;head=(Consumer*)malloc(S...
利用正则表达式提取TXT文本文档中的基因碱基对
需求:将txt文本里面的基因序列,提取出TDU和mcl开头的字符串 观察:每一行中的每一个字符串中间都隔开了一个空格 思路:将每一行中的每一个字符串进行分组,然后利用正则表达式进行匹配,最后输入到
链表
里面输出即可。 package mldn.cn.demo; import java.io.BufferedReader; import java.io.BufferedWriter; import...
IO 将
链表
内容
输入到
文件
中
【代码】IO 将
链表
内容
输入到
文件
中。
C++ 语言
65,211
社区成员
250,514
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章