?? book.cpp
字號:
#include<fstream>
using namespace std;
#include "book.h"
void OperBook::Append(fstream f)
{ int choice; int key; long num;
f.seekp( 0, ios::end ); //讀指針移到文件尾部
streampos posEnd = f.tellp() ; //記錄文件尾部位置
cout<<"*********************Appending*******************\n";
while (1)
{ cout<<"Please input choice:\n1: New Book No.\t"<< "2: Old Book No.\t"<< "0: Exit>>";
cin >> choice;
switch (choice)
{case 1: //追加記錄
{ cout << "No. (TP), Name, Amount: ";
cin >> book.TP>> book.bookName>> book.balance;
f.write( (char * ) & book , sizeof( bookData ) ) ; //寫入文件
break;
}
case 2: //修改記錄:
{ f.seekp( 0, ios::beg ); //寫指針移到文件頭
cout << "No.(TP) : "; cin >> key; //輸入書號
do //按照書號查找,讀數據賦給結構變量book
{ f.read((char * ) & book, sizeof(bookData));
} while (book.TP != key && (f.tellp() != posEnd));
if (book.TP == key ) //找到記錄
{ cout <<book. TP<< ' \t' << book.bookName<<' \t' << book.balance<< '\n';
cout << "Append number: \n? "; cin >> num;
if ( num>0 ) book.balance += num; //修改庫存量
else { cout<<"Input error occur"; continue; }
f.seekp(-long( sizeof( bookData )), ios::cur ) ;
f.write( ( char * ) & book , sizeof( bookData) ); //指針復位
cout << "Now total: \t" << book.balance << endl;
}
else cout << "Input No. error\n";
break;
}
case 0 : return;
}
}
}
void OperBook::Sale(fstream f)
{ int choice; int key; long num;
f.seekp( 0, ios::end ) ; streampos posEnd = f.tellp(); //記錄文件末尾位置
cout<<"************************Sale Registering**************\n";
while (1)
{ cout << "Please input operation choice: 1: Register "<< "0: Exit >>";
cin >> choice;
switch ( choice )
{ case 1 :
{ f.seekp( 0, ios::beg ); //從文件頭部開始檢索
cout << "No. (TP) : "; cin >> key;
do {f.read((char * ) & book, sizeof(bookData));
} while (book.TP!=key && f.tellp()!=posEnd );
if ( book.TP == key ) //找到
{ cout<<book. TP<<' \t'<<book.bookName<<' \t' <<book.balance<<endl;
cout << "Saled amount: "; cin >> num;
if ( num>0 && book.balance>=num )
book.balance -=num; //修改庫存
else continue;
f.seekp( - long( sizeof( bookData ) ), ios::cur ) ; //文件指針復位
f.write( (char * ) & book , sizeof( bookData ) ); //修改文件記錄
cout << "now store\t\t" << book.balance << endl;
}
else cout << "Input No. error \n";
break;
}
case 0 : return;
}
}
}
void OperBook::Inquire(fstream f)
{ int choice; int key;
f.seekg( 0, ios::end ); //讀指針移動到文件尾部
streampos posEnd = f.tellg() ; //記錄文件尾位置
cout<<"*******************Inquiring*********************"<<endl;
while ( 1 )
{ cout<<"Input the operation choice:\n1: According to No.\t"<< "2: Looking over\t"
<< "0: Exit >>"; cin >> choice;
switch ( choice )
{case 1:
{ f.seekg( 0, ios::beg );
cout << "No. (TP) : "; cin >> key;
do { f.read((char * ) & book, sizeof(bookData)) ;
} while ( book.TP != key && f.tellg() != posEnd ) ;
if ( book.TP == key )
cout<<book.TP<<' \t'<<book.bookName<< ' \t' << book.balance << '\n';
else { cout<<"Input No. Error"<<endl; continue; }
break;
}
case 2 : //瀏覽文件
{ f.seekg( 0, ios::beg ) ;
do //輸出所有記錄
{ f. read((char *) & book, sizeof(bookData)) ;
cout<<book.TP<<' \t' << book.bookName<< ' \t' <<book.balance <<'\n';
}while (book.TP != key );// while( book.TP !=key && f.tellp()!=posEnd );
break;
}
case 0 : return;
}
}
}
void OperBook::CreateTxt(fstream f)
{ fstream ftxt("booksFile.txt",ios::out) ; //以寫方式打開文件
f.seekg( 0,ios::end ) ;
streampos posEnd = f.tellg(); //記錄二進制文件末尾位置
f.seekg( 0, ios::beg ) ; //移動讀指針到文件頭
do{ f.read((char * ) & book , sizeof(bookData));
ftxt << book.TP << ' \t' << book.bookName << ' \t' << book.balance << endl;
} while ( f.tellg() != posEnd ) ;
ftxt.close() ; char answer , s[80] ;
cout << "Txt file created, whether look at the file or not?( Y/N ) \n";
cin >> answer;
if ( answer== 'y' || answer== 'Y' )
{ ftxt.open( "booksFile.txt",ios::in ); //重新用流打開文件
while( ! ftxt.eof() )
{ ftxt.getline( s, 80 ); cout << s << endl; } //按行顯示文本
}
ftxt.close() ; //關閉文本文件
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -