【C++ Primer】【学习笔记】【第八章】标准IO库之:文件的输入和输出
2015-01-19 23:51
447 查看
一、文件流对象的使用
1、fstream头文件定义了三种文件IO的类型:
2、与iostream类型相比,fstream增加了如下几个操作:
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、文件模式
2、文件模式的组合
注:以上所有模式的组合均可以加上ate模式,ate模式只会改变文件打开时的初始定位,在第一次读或写之前,将文件定位于文件末尾处。
习题8.6:读取文件
习题8.9:按行读取文件
习题8.10:按单词读取文件
1、fstream头文件定义了三种文件IO的类型:
类型 | 说明 |
ifstream | 从istream派生而来,提供读文件的功能; |
ofstream | 从ostream派生而来,提供写文件的功能; |
fstream | 从iostream派生而来,提供读写同一个文件的功能。 |
fstream增加的操作 | 说明 |
open(filename.c_str()); | 打开文件 |
close(); | 关闭文件 |
构造函数(filename.c_str()); | 打开文件,构造函数的形参为文件名 |
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操作 |
文件模式的组合 | 说明 |
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() { }
相关文章推荐
- 【C++ Primer】【学习笔记】【第八章】标准IO库之:输出缓冲区的管理
- C++ Primer学习笔记:标准输入/输出
- 【C++ Primer】【学习笔记】【第八章】标准IO库之:面向对象的标准库
- C++ Primer 学习笔记_26_标准I/O库(续) --文件的输入与输出、字符串流
- 【C++ Primer】【学习笔记】【第八章】标准IO库之:条件状态
- 【C++ Primer】【学习笔记】【第八章】标准IO库之:字符串流
- 学习笔记 (Shell的标准输入/标准输出/标准错误) [第四课]
- 《C++ Primer Plus》第17章 输入、输出和文件 学习笔记
- C++学习笔记:文件输入和输出
- 2012/2/11 《C++ Primer Plus》第十七章:输入、输出和文件 学习笔记
- C++学习笔记之输入、输出和文件
- c++primer学习笔记 ( 第八章 标准IO库)
- 【C++】学习笔记二十八——文件输入/输出
- c++ primer(第五版)学习笔记及习题答案代码版(第八章)IO库
- Python编程入门-第八章 输入和输出 -学习笔记
- C++Primer学习笔记第八章(8/18)标准IO库
- 【C++ Primer 学习笔记】chapter8 标准IO库
- C语言学习笔记_3基本的标准输入与输出
- Perl学习笔记(4)文件的输出输入
- C++语法基础--标准IO库--文件的输入和输出(fstream,ifstream,ofstream)