一、文件流对象的使用
1、fstream头文件定义了三种文件IO的类型:
类型
|
说明
|
ifstream | 从istream派生而来,提供读文件的功能; |
ofstream
|
从ostream派生而来,提供写文件的功能;
|
fstream
|
从iostream派生而来,提供读写同一个文件的功能。
|
2、与iostream类型相比,fstream增加了如下几个操作:
fstream增加的操作 |
说明
|
open(
filename.c_str());
|
打开文件
|
close();
|
关闭文件
|
构造函数(filename.c_str()); |
打开文件,构造函数的形参为文件名
|
3、检查文件打开是否成功
ifstream infile;
infile.open(filename.c_str());
if (!infile)
{
// 文件打开失败的处理
return;
}
4、将文件流与新文件重新捆绑
ifstream infile;
infile.open(filename.c_str());
infile.close();
infile.open(next_filename.c_str());
5、清除文件流的状态
重要:若需要重用文件流读写多个文件,必须在打开下一个文件之前使用clear()清除该流的状态。
ifstream infile;
infile.open(filename.c_str());
// 处理文件filename
infile.close();
infile.clear();
infile.open(next_filename.c_str());
// 处理文件next_filename
二、文件模式
1、文件模式
文件模式
|
说明
|
in
|
打开文件做读操作
|
out
|
打开文件做写操作
|
app
|
打开文件做写操作,并将文件定位在文件尾
|
ate
|
打开文件后立即将文件定位在文件尾
|
trunc
|
打开文件时情况已存在的文件流
|
binary
|
以二进制模式进行IO操作
|
2、文件模式的组合
文件模式的组合
|
说明
|
out
|
打开文件做读操作,删除文件中已有的数据
|
out | app
|
打开文件做写操作,在文件尾写入
|
out | trunc
|
与out模式相同
|
in
|
打开文件做读操作
|
in | out
|
打开文件做读、写操作,并定位于文件开头处
|
in | out | trunc
|
打开文件做读、写操作,删除文件中已有的数据
|
习题8.6:读取文件
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <string>
using namespace std;
istream& read_stream(istream& in)
{
string str;
while (in >> str, !in.eof())
{
if (in.bad())
{
throw runtime_error("IO stream corrupted!");
}
if (in.fail())
{
cout << "Read fail, try again." << endl;
in.clear();
in.ignore(2, ' ');
continue;
}
// str is ok, process it
cout << str << " ";
}
cout << endl;
in.clear();
return in;
}
int main()
{
string filename;
cout << "Enter filename: " << endl;
cin >> filename;
ifstream infile(filename.c_str());
if (!infile)
{
cout << "error: can't open file: " << filename << endl;
return -1;
}
read_stream(infile);
return 0;
}
[chapter8]$ ./a.out
Enter filename:
a.cpp
int main() { cout << a << endl; } int a() { } int b() { aaa; } int c() { }
习题8.9:按行读取文件
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
void file_to_vector(const string &filename, vector<string> &svec)
{
ifstream infile(filename.c_str());
if (!infile)
{
cout << "Error: open file(" << filename << ") fail." << endl;
return;
}
string s;
while (getline(infile, s))
{
svec.push_back(s);
}
infile.close();
if (infile.eof())
{
cout << "OK: read to eof." << endl;
}
if (infile.bad())
{
cout << "Error: system fail." << endl;
}
if (infile.fail())
{
cout << "Error: read fail." << endl;
}
return;
}
int main()
{
string filename;
cout << "Enter filename: " << endl;
cin >> filename;
vector<string> svec;
file_to_vector(filename.c_str(), svec);
cout << "vector's size: " << svec.size() << endl;
cout << "vector:" << endl;
for (vector<string>::iterator it = svec.begin(); it != svec.end(); ++it)
{
cout << *it << endl;
}
return 0;
}
[chapter8]$ ./a.out
Enter filename:
a.cpp
OK: read to eof.
Error: read fail.
vector's size: 20
vector:
int main()
{
cout << a << endl;
}
int a()
{
}
int b()
{
aaa;
}
int c()
{
}
习题8.10:按单词读取文件
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
void file_to_vector(const string &filename, vector<string> &svec)
{
ifstream infile(filename.c_str());
if (!infile)
{
cout << "Error: open file(" << filename << ") fail." << endl;
return;
}
string s;
while (infile >> s)
{
svec.push_back(s);
}
infile.close();
if (infile.eof())
{
cout << "OK: read to eof." << endl;
}
if (infile.bad())
{
cout << "Error: system fail." << endl;
}
if (infile.fail())
{
cout << "Error: read fail." << endl;
}
return;
}
int main()
{
string filename;
cout << "Enter filename: " << endl;
cin >> filename;
vector<string> svec;
file_to_vector(filename.c_str(), svec);
cout << "vector's size: " << svec.size() << endl;
cout << "vector:" << endl;
for (vector<string>::iterator it = svec.begin(); it != svec.end(); ++it)
{
cout << *it << endl;
}
return 0;
}
[chapter8]$ ./a.out
Enter filename:
a.cpp
OK: read to eof.
Error: read fail.
vector's size: 22
vector:
int
main()
{
cout
<<
a
<<
endl;
}
int
a()
{
}
int
b()
{
aaa;
}
int
c()
{
}