书:C++大学教程(第七版)
1. ios::app 将输出数据添加到文件的结尾
ios::ate 将一个文件打开作为输出文件,并移动到文件尾(一般用来为一个文件添加数据)。可以在文件的任何位置写数据
ios::in 打开一个文件作为输入文件
ios::out 打开一个文件作为输出文件
ios::trunc 如果文件有内容则将文件内容丢弃(这是ios::out的默认设置)
ios::binary 打开一个文件进行二进制(也就是非文本方式)输入或输出
在打开一个已存在的文件进行输出(ios::out)时必须小心,尤其是在想要保存这个文件的内容的情况下,因为文件原有的内容会在没有警告的情况下被丢弃。
2. ifstream类对象的默认打开为输入模式
3. 重定位
istream中,函数seekg(0)
ostream中,函数seekp(0)
ios::beg,默认方式,相对于流的开始位置进行定位
ios::cur,相对于当前流的位置进行定位
ios::end,相对于流的即为进行定位
例:
fileObject.seekg(n);
fileObject.seekg(n, ios::cur)
fileObject.seekg(n,ios::end)
fileObject.seekg(0,ios::end)
成员函数tellg和tellp分别用来返回当前的“get”和“put”指针的位置;location = fileObject.tellg();
4. 创建随机存取文件
reinterpret_cast的使用是与编译器相关的程序在不同的平台上运行起来可能不一样。除非有绝对的必要,都不应该使用reinterpret_cast运算符。
#include <iostream>
#include <fstream>
#include <cstdlib> // exit function prototype
#include "ClientData.h" // ClientData class definition
using namespace std;
int main()
{
ofstream outCredit( "credit.dat", ios::out | ios::binary );
// exit program if ofstream could not open file
if ( !outCredit )
{
cerr << "File could not be opened." << endl;
exit( 1 );
} // end if
ClientData blankClient; // constructor zeros out each data member
// output 100 blank records to file
for ( int i = 0; i < 100; i++ )
outCredit.write( reinterpret_cast< const char * >( &blankClient ),
sizeof( ClientData ) );
} // end main
- 随机写入文件
#include <iostream>
#include <fstream>
#include <cstdlib> // exit function prototype
#include "ClientData.h" // ClientData class definition
int main()
{
int accountNumber;
string lastName;
string firstName;
double balance;
fstream outCredit( "credit.dat", ios::in | ios::out | ios::binary );
// exit program if fstream cannot open file
if ( !outCredit )
{
cerr << "File could not be opened." << endl;
exit( 1 );
} // end if
cout << "Enter account number (1 to 100, 0 to end input)\n? ";
// require user to specify account number
ClientData client;
cin >> accountNumber;
// user enters information, which is copied into file
while ( accountNumber > 0 && accountNumber <= 100 )
{
// user enters last name, first name and balance
cout << "Enter lastname, firstname, balance\n? ";
cin >> lastName;
cin >> firstName;
cin >> balance;
// set record accountNumber, lastName, firstName and balance values
client.setAccountNumber( accountNumber );
client.setLastName( lastName );
client.setFirstName( firstName );
client.setBalance( balance );
// seek position in file of user-specified record
outCredit.seekp( ( client.getAccountNumber() - 1 ) *
sizeof( ClientData ) );
// write user-specified information in file
outCredit.write( reinterpret_cast< const char * >( &client ),
sizeof( ClientData ) );
// enable user to enter another account
cout << "Enter account number\n? ";
cin >> accountNumber;
} // end while
} // end main
- 随机存取文件顺序读取数据
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib> // exit function prototype
#include "ClientData.h" // ClientData class definition
using namespace std;
void outputLine( ostream&, const ClientData & ); // prototype
int main()
{
ifstream inCredit( "credit.dat", ios::in | ios::binary );
// exit program if ifstream cannot open file
if ( !inCredit )
{
cerr << "File could not be opened." << endl;
exit( 1 );
} // end if
cout << left << setw( 10 ) << "Account" << setw( 16 )
<< "Last Name" << setw( 11 ) << "First Name" << left
<< setw( 10 ) << right << "Balance" << endl;
ClientData client; // create record
// read first record from file
inCredit.read( reinterpret_cast< char * >( &client ),
sizeof( ClientData ) );
// read all records from file
while ( inCredit && !inCredit.eof() )
{
// display record
if ( client.getAccountNumber() != 0 )
outputLine( cout, client );
// read next from file
inCredit.read( reinterpret_cast< char * >( &client ),
sizeof( ClientData ) );
} // end while
} // end main
// display single record
void outputLine( ostream &output, const ClientData &record )
{
output << left << setw( 10 ) << record.getAccountNumber()
<< setw( 16 ) << record.getLastName()
<< setw( 11 ) << record.getFirstName()
<< setw( 10 ) << setprecision( 2 ) << right << fixed
<< showpoint << record.getBalance() << endl;
} // end function outputLine
- 打开文件模板
ofstream& open_file(ofstream &out, const string &file)
{
out.close();
out.clear();
out.open(file.c_str());
return out;
}
- istringstream
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string str = "i an a boy";
istringstream is(str);
string s;
while (is >> s)
{
cout << s << endl;
}
system("pause");
return 0;
}