首先,我試圖創建一個文件並寫入它,它不讓我使用「< <」寫入文件,而第二部分我試圖從文件讀取數據,但我不確定這是正確的方式,因爲我想將數據保存到對象中,以便稍後在程序中使用對象。任何幫助或建議非常感謝。在此先感謝如何使用文件I/O C++
void Employee::writeData(ofstream&)
{
Employee joe(37," ""Joe Brown"," ""123 Main ST"," ""123-6788", 45, 10.00);
Employee sam(21,"\nSam Jones", "\n 45 East State", "\n661-9000",30,12.00);
Employee mary(15, "\nMary Smith","\n12 High Street","\n401-8900",40, 15.00);
ofstream outputStream;
outputStream.open("theDatafile.txt");
outputStream << joe << endl << sam << endl << mary << endl;
//it says that no operator "<<"matches this operands, operands types are std::ofstream<<employee
outputStream.close();
cout<<"The file has been created"<<endl;
}
void Employee::readData(ifstream&)
{
//here im trying to open the file created and read the data from it, but I'm strugguling to figure out how to read the data and save it into de class objects.
string joe;
string sam;
string mary;
ifstream inputStream;
inputStream.open("theDatafile.txt");
getline(inputStream, joe);
getline(inputStream, sam);
getline(inputStream, mary);
inputStream.close();
}
您需要定義一個'運營商<<'爲'員工'。 – 0x499602D2
@carlpett:'<<'用於輸出,'>>'用於輸入 –
@Ken:哇,我一定累了。沒有更多的SO今晚... – carlpett