文件的读问题!!要交作业,很急的!

anxiner 2005-09-12 10:10:02
我有一外部文件a.txt,里面有一些信息:
编号 姓名 性别 年龄 工资
001 guo 男 21 3000
021 li 女 20 2500
。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。
怎样把它读到一链表中,想返回首指针h??
Workernode *read(){?????怎样写}//想返回首指针h(WorkerNode *h)

我在程序内定义啦一个类WorkerNode

class WorkerNode{

public:
int Num;
char Name[10];
char Sex[5];
int Age;
float S;
WorkerNode*next;
};
///我写的整个程序如下:
流这块学的不好,书上讲的也不是很细。
本人用的是VC++6.0 那位帮我下忙,尽量用流里的简单的代码写!!!谢啦!!!

#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<fstream.h>


class WorkerNode{

public:
int Num;
char Name[10];
float S;
WorkerNode*next;
};
void Print ( WorkerNode *h)
{
WorkerNode *p;
p=h;
cout<<"output data;";

cout<<"编号"<<'\t'<<"姓名"<<'\t'<<"工资"<<'\n';
while(p!=0){
cout<<'\t'<<'\t'<<p->Num<<'\t'<<p->Name<<'\t'<<p->S<<'\n';
p=p->next;
}
cout<<'\n';
}
WorkerNode *Insert (WorkerNode *head=0,WorkerNode *p=0)
{
WorkerNode *p1,*p2;
if(head==0){
head=p;
p->next=0;
return head;
}
/*else{//无序链表
p1=head;
while(p1->next!=0) {p1=p1->next;}/////此处出错!!!!空链表不幸
p1->next=p;
p->next=0;
}*/
if(head->Num>=p->Num){
p->next=head;head=p;
return head;
}
p2=p1=head;
while(p2->next&&p2->Num<p->Num){
p1=p2;p2=p2->next;
}
if(p2->Num<p->Num){
p2->next=p;p->next=0;
}
else{
p->next=p2;p1->next=p;
}
return head;
}
WorkerNode * Delete_one_node(WorkerNode*head=0,int num=0)
{
WorkerNode *p1,*p2;
if(head==0){
cout<<"List is empty,no Node can delete \n";
return (0);
}
if(head->Num==num){
p1=head;
head=head->next;
delete p1;
cout<<"delete onenode\n";
}
else {
if(head->next==0){ cout<<"List has no the node deleted \n";}
else{
p1=head;
p2=head->next;
while(p2->Num!=num && p2->next!=0){
p1=p2;p2=p2->next;}
if(p2->Num==num){
p1->next=p2->next;
delete p2;
cout<<"delete one Node \n";
}
else{ cout<<"List has no the node deleted \n";}
}
}
return head;
}



void deletechain(WorkerNode*h)
{
WorkerNode *p1;
while(h){
p1=h;
h=h->next;
delete p1;
}
}
WorkerNode *Create_sort()//有序连表
{
WorkerNode *p1,*h=0;
int num;
cout<<"sort list !input Num,end with -1:\n";
cin>>num;
while(num!=-1){
p1=new WorkerNode;
p1->Num=num;
cout<<"input Name:";
cin>>p1->Name;
cout<<"input S:";
cin>>p1->S;
h=Insert(h,p1);
cin>>num;
}
return h;
}
WorkerNode *ModityName(WorkerNode*head=0,int num=0,char *na=0)
{
WorkerNode *p1,*p2;
if(head==0){
cout<<"List is empty,no Node can modity \n";
return (0);
}
if(head->Num==num){
strcpy(head->Name,na);
cout<<"modity one node\n";
}
else {
if(head->next==0) cout<<"List has no the node modityed \n";//*********//
else{
p1=head;
p2=head->next;
while(p2->Num!=num&&p2->next!=0){
p1=p2;p2=p2->next;}
if(p2->Num==num){
strcpy(p2->Name,na);

cout<<"modity one Node \n";
}
else cout<<"List has no the node modityed \n";
}
}
return head;
}
//static int w=0;
void write(WorkerNode *h)
{
WorkerNode *p;
p=h;

char filename[20];
cout<<"输入文件名:\n";
cin>>filename;
ofstream outfile(filename);
if(!outfile)
{
cout<<"不能建立该文件!";
exit(1);
}
outfile<<"编号"<<'\t'<<"姓名"<<'\t'<<"工资"<<'\n';
while(p!=0){
outfile<<p->Num<<'\t'<<p->Name<<'\t'<<p->S<<'\n';
p=p->next;//w++;
}
cout<<'\n';
}



//??????????????????/////////
Workernode *read()
{
}
//????????????????///////////////////////////

void main(void)
{ int num,A;
WorkerNode *head=0,*p3=0;
int n;
//char na[10];
//float s;
cout<<"***********************职工管理系统执行程序******************\n";
while(1){

cout<<"第一次输入职工基本信息请按1\n";
cout<<"删除某职工基本信息请按2\n";
cout<<"增加新职工请按3\n";
cout<<"输出全体职工基本信息请按4\n";
cout<<" \t修改信息请按5\n";
cout<<" \t读入文件请按6\n";
cout<<" \t写入文件请按7\n";
cout<<"\t退出程序请按8\n";
cin>>A;
switch(A){
case 1:
head=Create_sort();break;
case 2:
cout<<"input the num of node deleted :\n";
cin>>num;
head=Delete_one_node(head,num);break;
case 3:
p3=new WorkerNode;
cout<<"input Num:\n";
cin>>n;
p3->Num=n;
cout<<"input Name:\n";
cin>>p3->Name;
cout<<"input S:\n";
cin>>p3->S;
//strcpy(p3->Name,na);
//p3->S=s;
head=Insert(head,p3);break;
case 4:
Print(head);break;
case 8:exit(1);break;
case 5:
{char na[10];int num;//*na --->nas[10]
int f;
cout<<"修改姓名请按11:\n";
cout<<"修改工资请按22:\n";
cout<<" 退出请按33:\n";
cin>>f;
switch(f){
case 11:
cout<<" input Num:";
cin>>num;
cout<<"NAME:";
cin>>na;
ModityName(head,num, na);break;
case 33:exit(1);break;}
//default:cout<<"非法操作!";
}break;
case 6:read(head);break;
case 7:write(head);break;
}
}
}







...全文
250 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
anxiner 2005-09-17
  • 打赏
  • 举报
回复
今天 在线等。。。。。。。
anxiner 2005-09-16
  • 打赏
  • 举报
回复
咋没人回啊。。。。。。
anxiner 2005-09-15
  • 打赏
  • 举报
回复
楼上说的和这有关吗?
谁写下我再加分!!!!
pizicai105 2005-09-13
  • 打赏
  • 举报
回复
CStdioFile mFile;
mFile.SetLength(0);//清空原文件里所有内容
mFile.SeekToEnd()//将指针跳到文件末,再往里写内容就是“追加写”了!
//********
SetLength(0);//清空内容
//***********
CStdioFile mFile;
CFileException mExcept;
mFile.Open(strLogFilePath,CFile::modeWrite,&mExcept);
CString str;
str.Format("%s %s %s %s\n",s1,s2,s3,s4);
mFile.SeekToEnd();//追加写
mFile.WriteString(str);
mFile.Close();
echoxue 2005-09-13
  • 打赏
  • 举报
回复
UP
anxiner 2005-09-13
  • 打赏
  • 举报
回复
我就是读的代码不会写。。。
谁写下我再加分就是啦。。。
to:ragelius
"处理好读的位置和结构的对应关系就行" 你能帮忙写下,我流这部分学的不好。。。
anxiner 2005-09-13
  • 打赏
  • 举报
回复
楼上的那就帮忙写下读的代码!谢谢啦。。。
tidyduck 2005-09-13
  • 打赏
  • 举报
回复
作业题啊?还这么少的分?
nkwesley 2005-09-13
  • 打赏
  • 举报
回复
可以试试用CSV文件格式
rageliu 2005-09-13
  • 打赏
  • 举报
回复
呵呵!!!读进结构存结构进list就好了
处理好读的位置和结构的对应关系就行
xqk 2005-09-13
  • 打赏
  • 举报
回复
整的也太乱了吧
BuZhang_AP97091 2005-09-12
  • 打赏
  • 举报
回复
连作业都不会做了,以后怎么做项目?
anxiner 2005-09-12
  • 打赏
  • 举报
回复
大虾们,帮忙解决下。。。

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧