在這個函數中,我試圖從鏈表中的信息寫入到文件我怎樣寫鏈表作爲一個對象到一個文件
class Employer: public People{
void tryWrite(){
ofstream file("Employer.dat",ios::out|ios::app| ios::binary);
if(file.is_open()){
file<<getFirstName()<<" "<<getLastName()<<" " << companyName<<" "<<position<<" "<<getTelHome()<<" "<<getTelMobile()<<" " <<email<<" "<<getAddress()<< endl;
file.close();
}
}
};
#endif
這是僱主類
鏈表操縱在WriteFile函數我試圖通過鏈接列表遍歷並調用Employer.h的tryWrite函數寫在鏈接列表的實際數據保存到文件這一點
class EmployerList
{
private:
Employer *head;//employee pointer(head pointer)
public:
void writeFile(){
Employer *temp = head;
while (temp != NULL)
{
temp->tryWrite();//Employee's display method called here
temp = temp->getNext();
}
}
在此閱讀功能中,我想按以下順序讀取數據姓名:Jane Brown公司名稱:Lime職位:店員地址: New Kingston首頁#:876-466-8778手機號碼:543-455-6344電子郵件: [email protected]
而是直接從文件中讀取數據,珍妮·布朗石灰店員 新金士頓876-466-8778 543-455-6344 [email protected]
當它到達的地址它會顯示爲:地址:新
而不是:地址:New Kingston
T他零空間中創造的問題,我不知道如何得到它 做我想做什麼
無效TestRead(){
ifstream tryRead("em.dat", ios::in|ios::binary);
string Email, cName, pos,fName,lName,addr, tHome,tMobile;
while(tryRead>>fName>>lName>>cName>>pos>>addr>>tHome>>tMobile){
cout<<"Name: "<<fName<<lName<<endl;
cout<<"Comapny's Name: "<<cName<<endl;
cout<<"Position: "<<pos<<endl;
cout<<"Address: "<<addr<<endl;
cout<<"Home#: "<<tHome<<endl;
cout<<"Mobile#: "<<tMobile<<endl;
tryRead.close();
}
}
};
#endif
這是主要的,其填充數據
鏈表鏈表中填充數據#include <iostream>
#include <string>
#include "Employer.h"
#include "EmployerList.h"
using namespace std;
void main()
{
EmployerList em;
em.AddNode("[email protected]", "LIME", "Manager", "Tashauna", "Simon", "New Kingston", "876-985-8337", "876-405-3536");
em.AddNode("[email protected]", "NOKIA", "CEO", "Keric", "McDonald", "Europe", "0411-789-6548", "985-257-1111");
經過EmployerList.h寫文件函數被調用
em.writeFile();
em.TestRead();
system("pause");
}
這是很難幫助你,如果你必須通過所有的代碼閱讀。不要發佈超過必要的代碼。 – 2012-03-25 08:27:45
@ Zyx2000我拿出不必要的代碼 – 2012-03-25 09:40:30
您是否從電子郵件複製並粘貼您的問題?你爲什麼要把文字放在文字的前面?它導致文字顯示爲「引號」的【如何寫一個鏈表對象文件] – 2012-03-29 04:22:58