一个链表的问题?

ICAC2008 2004-04-03 12:20:14
请问怎么把一个链表中的所有内容保存到一个文件上??如一下链表:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class Customer
{
private:
string cCode;
char cName[30];
char cAddress[50];
public:
void getData(const string &code)
{
cCode=code;
cin.ignore();
cout<<endl<<"Enter Customer Name: ";
cin.getline(cName,30);
cout<<endl<<"Enter Customer Address: ";
cin.getline(cAddress,50);
}

void showData()
{
cout<<"****The Customer Details are****"<<endl;
cout<<"Customer Code is: "<<cCode<<endl;
cout<<"Customer Name is: "<<cName<<endl;
cout<<"Cusromer Address is: "<<cAddress<<endl;
}

string getCode()
{
return cCode;
}
};

struct Node
{
public:
Customer obj;
Node *NEXT;

Node(const string &s,Node *n=NULL)
{
NEXT=n;
obj.getData(s);
}
};

class List
{
private:
Node *START,
*CURRENT,
*PRECEDE;
public:
List();
~List()
{
destroy();
}
void addNode(const string &s);
bool delNode(const string &s);
bool queryNode(const string &s);
void displayNode(const string &s);
void traverse();
void destroy();
};

List::List()
{
START=CURRENT=PRECEDE=NULL;
}

void List::destroy()
{
while(START!=NULL)
{
CURRENT=START;
START=START->NEXT;
delete CURRENT;
}
START=CURRENT=PRECEDE=NULL;
}

void List::addNode(const string &s)
{
if(START==NULL || s<=START->obj.getCode())
{
START=new Node(s,START);
return;
}
Node *prev,*curr;
for(prev=curr=START;curr!=NULL && s>curr->obj.getCode();prev=curr,curr=curr->NEXT)
{}
Node *n=new Node(s,curr);
prev->NEXT=n;
}

bool List::queryNode(const string &s)
{
for(PRECEDE=CURRENT=START;CURRENT!=NULL && s!=CURRENT->obj.getCode();PRECEDE=CURRENT,CURRENT=CURRENT->NEXT)
{}
return (CURRENT!=NULL);
}

bool List::delNode(const string &s)
{
if(queryNode(s)==false)
{
return false;
}
PRECEDE->NEXT=CURRENT->NEXT;
if(CURRENT==START)
{
START=START->NEXT;
}
delete CURRENT;
return true;
}

void List::displayNode(const string &s)
{
CURRENT->obj.showData();
}

void List::traverse()
{
for(Node *temp=START;temp!=NULL;temp=temp->NEXT)
{
temp->obj.showData();
cout<<endl;
}
}


int main()
{
List list;
char ch;
while(1)
{
cout << endl << "1. Enter Customer Details" << endl;
cout << "2. Delete Customer Details from list " << endl;
cout << "3. Search for a Customer " << endl;
cout << "4. Display Details of all Customers" << endl;
cout << "5. Exit" << endl;
cout << endl << "Enter choice :: ";
cin >> ch;
switch(ch)
{
case '1':
{
cout << endl << "Enter Customer Code: ";
string cCode;
cin.ignore();
cin>>cCode;
list.addNode(cCode);
}
break;
case'2':
{
cout << endl << "Enter Customer Code: ";
string cCode;
cin.ignore();
cin>>cCode;
if(list.delNode(cCode) == false)
{
cout << "Customer not found" << endl;
}
}
break;
case '3':
{
cout << endl << "Enter a Customer Code: ";
string cCode;
cin.ignore();
cin>>cCode;
if(list.queryNode(cCode) == false)
{
cout << "Customer not found" << endl;
}
else
{
cout << "Customer found in list\n\n"<<endl;
list.displayNode(cCode);
}
}
break;
case '4':
list.traverse();
break;
case '5':
exit(0);
break;
default:
cout << endl << "Enter a correct choice ";
break;
}
}
return 0;
}
...全文
71 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
rorot 2004-04-06
  • 打赏
  • 举报
回复
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

// 从文件中最大读入数据量
const int MAXSIZE = 1024;
// 写入文件数据缓冲区
char buffer_Write[] = "This is a test of '_write' function";
// 读取文件数据缓冲区
char buffer_Read[MAXSIZE];

int main( void )
// write(), read()使用示例
{
// 文件句柄
int in_file;
// 实际写入或者读取得数据量
unsigned bytes;

// 打开文件,获得句柄
if( (in_file = _open( "e:\\temp\\002.txt", _O_RDWR | _O_CREAT,
_S_IREAD | _S_IWRITE )) == -1 )
{
perror( "Open File Error\n" );
return -1;
}

// 读取数据
if ( (bytes = _read( in_file, buffer_Read, MAXSIZE )) == -1 )
perror( "Read failed\n" );

// 打印出文件数据
printf( "%s\n", buffer_Read );


// 往文件中写入数据
if(( bytes = _write( in_file, buffer_Write, sizeof( buffer_Write ))) == -1 )
perror( "Write failed\n" );
else
printf( "Wrote %u bytes to file\n", bytes );

// 关闭文件句柄
_close( in_file );

return 0;
}
ICAC2008 2004-04-06
  • 打赏
  • 举报
回复
有用啊,具体的以一个一个对象往文件里写怎么操作?
danielpan 2004-04-05
  • 打赏
  • 举报
回复
有必要保存指针吗,里面的数据在文件中对你有用吗?

你在存的时候按照顺序遍历链表,遍历的同时依次保存数据项到文件中.

读的时候先建一个空链表,每读一项就在链表上加一项就可以了阿.
ICAC2008 2004-04-05
  • 打赏
  • 举报
回复
有没有什么实例啊
古布 2004-04-05
  • 打赏
  • 举报
回复
完全可以!
ICAC2008 2004-04-05
  • 打赏
  • 举报
回复
up
ICAC2008 2004-04-04
  • 打赏
  • 举报
回复
1. 对文件的读写操作我会,但我不知道在这个程序中怎么加进去!
2. 不过要谢谢rorot() !你的回答太具体了,这就是我想要的!
3. 但是还有一个问题想想问问大家,就是我可不可以用write()函数和read()函数来做呢?
4. write()和read()都是2进制函数,具体怎么理解2进制函数呢?
5. 以及和使用现在的方法有什么区别吗?
angelo23 2004-04-03
  • 打赏
  • 举报
回复
写一个函数,遍历链表,把每个元素写入文件咯……这个……楼主是不知道怎么进行文件读写操作还是什么?
#include <fstream>
ofstream ofile("out.txt");
//...
ofile << obj.a; //这个就看你的每个结点怎么实现的了,或者你重载<<操作符也可以。在形式上用法其实和cout基本一样
psbeond 2004-04-03
  • 打赏
  • 举报
回复
同意楼上
fireseed 2004-04-03
  • 打赏
  • 举报
回复
按顺序把链表的数据指针的数据读出来,用fstream写入。

读出的时候先申请内存,再读入,再加入链表即可
rorot 2004-04-03
  • 打赏
  • 举报
回复
帮你把代码改了一下,可以存储文件了.
-------------------------------------------
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class Customer
{
private:
string cCode;
char cName[30];
char cAddress[50];
public:
void getData(const string &code)
{
cCode=code;
cin.ignore();
cout<<endl<<"Enter Customer Name: ";
cin.getline(cName,30);
cout<<endl<<"Enter Customer Address: ";
cin.getline(cAddress,50);
}

void showData()
{
cout<<"****The Customer Details are****"<<endl;
cout<<"Customer Code is: "<<cCode<<endl;
cout<<"Customer Name is: "<<cName<<endl;
cout<<"Cusromer Address is: "<<cAddress<<endl;
}

////////////////////////////////
/////存储函数
///////////////////////////////
void SaveData(ofstream &out_file)
{
out_file<<"****The Customer Details are****"<<endl;
out_file<<"Customer Code is: "<< cCode <<endl;
out_file<<"Customer Name is: "<< cName <<endl;
out_file<<"Cusromer Address is: "<< cAddress <<endl;
out_file<< endl;
}

string getCode()
{
return cCode;
}
};

struct Node
{
public:
Customer obj;
Node *NEXT;

Node(const string &s,Node *n=NULL)
{
NEXT=n;
obj.getData(s);
}
};

class List
{
private:
Node *START,
*CURRENT,
*PRECEDE;
public:
List();
~List()
{
destroy();
}
void addNode(const string &s);
bool delNode(const string &s);
bool queryNode(const string &s);
void displayNode(const string &s);
void traverse();
void destroy();
void SaveFile(const char *filePath);
};

List::List()
{
START=CURRENT=PRECEDE=NULL;
}

void List::destroy()
{
while(START!=NULL)
{
CURRENT=START;
START=START->NEXT;
delete CURRENT;
}
START=CURRENT=PRECEDE=NULL;
}

void List::addNode(const string &s)
{
if(START==NULL || s<=START->obj.getCode())
{
START=new Node(s,START);
return;
}
Node *prev,*curr;
for(prev=curr=START;curr!=NULL && s>curr->obj.getCode();prev=curr,curr=curr->NEXT)
{}
Node *n=new Node(s,curr);
prev->NEXT=n;
}

bool List::queryNode(const string &s)
{
for(PRECEDE=CURRENT=START;CURRENT!=NULL && s!=CURRENT->obj.getCode();PRECEDE=CURRENT,CURRENT=CURRENT->NEXT)
{}
return (CURRENT!=NULL);
}

bool List::delNode(const string &s)
{
if(queryNode(s)==false)
{
return false;
}
PRECEDE->NEXT=CURRENT->NEXT;
if(CURRENT==START)
{
START=START->NEXT;
}
delete CURRENT;
return true;
}

void List::displayNode(const string &s)
{
CURRENT->obj.showData();
}

void List::traverse()
{
for(Node *temp=START;temp!=NULL;temp=temp->NEXT)
{
temp->obj.showData();
cout<<endl;
}
}


/////////////////////////////////////////////
// 文件存储函数
////////////////////////////////////////////////
void List::SaveFile(const char *filePath)
{
// 输出文件流
ofstream out_file(filePath);
if ( out_file == NULL )
{
cout << filePath << " Open Fail..\n";
return;
}

// 文件打开成功
for(Node *temp=START;temp!=NULL;temp=temp->NEXT)
{
temp->obj.SaveData(out_file);
out_file<<endl;
}

// 关闭文件流
out_file.close();
}

int main()
{
List list;

// 文件输出路径
const char filePath[] = "E:\\temp\\test.txt";

char ch;
while(1)
{
cout << endl << "1. Enter Customer Details" << endl;
cout << "2. Delete Customer Details from list " << endl;
cout << "3. Search for a Customer " << endl;
cout << "4. Display Details of all Customers" << endl;
cout << "5. Save the data to a file." << endl;
cout << "6. Exit" << endl;
cout << endl << "Enter choice :: ";
cin >> ch;
switch(ch)
{
case '1':
{
cout << endl << "Enter Customer Code: ";
string cCode;
cin.ignore();
cin>>cCode;
list.addNode(cCode);
}
break;
case'2':
{
cout << endl << "Enter Customer Code: ";
string cCode;
cin.ignore();
cin>>cCode;
if(list.delNode(cCode) == false)
{
cout << "Customer not found" << endl;
}
}
break;
case '3':
{
cout << endl << "Enter a Customer Code: ";
string cCode;
cin.ignore();
cin>>cCode;
if(list.queryNode(cCode) == false)
{
cout << "Customer not found" << endl;
}
else
{
cout << "Customer found in list\n\n"<<endl;
list.displayNode(cCode);
}
}
break;
case '4':
list.traverse();
break;
// 储存文件
case '5':
list.SaveFile(filePath);
break;

case '6':
exit(0);
break;
default:
cout << endl << "Enter a correct choice ";
break;
}
}
return 0;
}

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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