
void Reserve::Add() //訂房
{
ofstream ofs("d:\\hotel system.txt", ios::out | ios::app);
if (ofs.is_open())
{
Reserve e;
cout << "--------------------------------------------" << endl;
cout << "請輸入訂房人數: " << endl;
int addnum = 0;
cin >> addnum;
cout << "請輸入入住者的姓名、身份證號、入住日期、所訂房型:" << endl;
for (int j = 0; j < addnum; j++)
{
cin >> e;
ofs << e;
}
ofs.close();
}
void Reserve::show() //查看訂房記錄
{
fstream fd("d:\\Hotel system.txt", ios::in);
int ch;//定義一個字符
ch = fd.get();//從檔案中讀取一個字符
if (ch == EOF)//如果一個字符都沒讀取到,表示檔案為空的
{
cout << "空白檔案,無法讀取!" << endl;
}
else
{
cout << "--------------------------------------------" << endl;
cout << " 姓名\t" << "身份證號\t" << "入住日期\t"<<"所訂房型" << endl;
int n = 0; string str;
Reserve *e = new Reserve[50];
while(getline(fd,str))
{
cout << " ";
cout<<str<<endl;
}
fd.close();
}
}
以上是對該檔案夾輸入輸出的代碼,課設馬上要交了,求賜教,謝謝!!
uj5u.com熱心網友回復:
檔案可能不是空白的,或者有BOM頭造成的。建立一個完全空白的0位元組的檔案試試吧。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/250781.html
標籤:C++ 語言
