C++中文件的输入跟输出
C++中文件的输入和输出
学习C++ http://www.maiziedu.com/course/379/,我们都知道C++中文件的输入和输出机制和屏幕上输入输出相似, 主要区别在于需要显式地打开和关闭文件. 对文件进行操作时会涉及到5个相关的类:
· fstreambase: 公共基类, 具体文件操作中不会使用这个类.
· ifstream: 从istream派生, 负责文件的读取.
· ofstream: 从ostream派生, 负责文件的写入.
· fstream: 从fstreambase和iostream中派生, 负责对文件进行写入和读取.
· filebuf: 从streambuf中派生, 用来提供缓冲支持.
下面的代码从一个txt读取字符(数字和字母):
#include #include using namespace std;int main(int argc, const char * argv[]) {
ifstream inf;
//打开文件
inf.open("/Users/zhang/Desktop/C++/fileIO/1.txt");
if (!inf) {
cerr << "打开文件失败!" << endl;
return -1;
}
char x;
while (inf >> x) {
cout << x;
}
cout << endl;
//关闭文件
inf.close();
return 0;
}
下面的代码向文件中写入1~10共10个数字:
#include #include using namespace std;int main(int argc, const char * argv[]) {
ofstream onf;
onf.open("/Users/zhang/Desktop/C++/fileIO/2.txt");
if (!onf) {
cerr << "打开文件失败!" << endl;
return -1;
}
for (int i = 0; i <= 10; i++) {
onf << i << endl;
}
onf.close();
return 0;
}
上面两个例子中的ifstream和ofstream都使用了open函数来打开文件, 其实通过ifstream和ofstream的构造函数可以实现相同的功能:
ifstream inf("/Users/zhang/Desktop/C++/fileIO/1.txt");
或ofstream onf("/Users/zhang/Desktop/C++/fileIO/2.txt");
此外, 还有另外一个构造函数, 可以指定文件打开的模式:
· ios::in : 打开一个可读取文件.
· ios::out : 打开一个可写入文件.
· ios::app : 在文件末尾追加数据.
· ios::trunk : 删除文件原来的内容.
· ios::nocreate : 如果要打开文件不存在, 不会创建同名文件.
· ios::noreplace : 如果要打开的文件已存在, 不会对已存在的文件进行替换.
· ios::binary : 以二进制形式打开文件.
通过’|’操作符可以指定多个打开模式. 下面是一个使用组合模式的例子:
#include #include using namespace std;int main(int argc, const char * argv[]) {
fstream File("/Users/zhang/Desktop/C++/fileIO/1.txt",ios::in | ios::app);
if (! File) {
cerr << "打开文件失败!" << endl;
return -1;
}
//写入文件
File << "hello world";
//将文件指针移动至文件开头
File.seekg(ios::beg);
//输入文件内容
char str[100];
File.getline(str, 100);
cout << str << endl;
File.close();
return 0;
}
文章来源:Devzhang
学习C++ http://www.maiziedu.com/course/379/,我们都知道C++中文件的输入和输出机制和屏幕上输入输出相似, 主要区别在于需要显式地打开和关闭文件. 对文件进行操作时会涉及到5个相关的类:
· fstreambase: 公共基类, 具体文件操作中不会使用这个类.
· ifstream: 从istream派生, 负责文件的读取.
· ofstream: 从ostream派生, 负责文件的写入.
· fstream: 从fstreambase和iostream中派生, 负责对文件进行写入和读取.
· filebuf: 从streambuf中派生, 用来提供缓冲支持.
下面的代码从一个txt读取字符(数字和字母):
#include #include using namespace std;int main(int argc, const char * argv[]) {
ifstream inf;
//打开文件
inf.open("/Users/zhang/Desktop/C++/fileIO/1.txt");
if (!inf) {
cerr << "打开文件失败!" << endl;
return -1;
}
char x;
while (inf >> x) {
cout << x;
}
cout << endl;
//关闭文件
inf.close();
return 0;
}
下面的代码向文件中写入1~10共10个数字:
#include #include using namespace std;int main(int argc, const char * argv[]) {
ofstream onf;
onf.open("/Users/zhang/Desktop/C++/fileIO/2.txt");
if (!onf) {
cerr << "打开文件失败!" << endl;
return -1;
}
for (int i = 0; i <= 10; i++) {
onf << i << endl;
}
onf.close();
return 0;
}
上面两个例子中的ifstream和ofstream都使用了open函数来打开文件, 其实通过ifstream和ofstream的构造函数可以实现相同的功能:
ifstream inf("/Users/zhang/Desktop/C++/fileIO/1.txt");
或ofstream onf("/Users/zhang/Desktop/C++/fileIO/2.txt");
此外, 还有另外一个构造函数, 可以指定文件打开的模式:
· ios::in : 打开一个可读取文件.
· ios::out : 打开一个可写入文件.
· ios::app : 在文件末尾追加数据.
· ios::trunk : 删除文件原来的内容.
· ios::nocreate : 如果要打开文件不存在, 不会创建同名文件.
· ios::noreplace : 如果要打开的文件已存在, 不会对已存在的文件进行替换.
· ios::binary : 以二进制形式打开文件.
通过’|’操作符可以指定多个打开模式. 下面是一个使用组合模式的例子:
#include #include using namespace std;int main(int argc, const char * argv[]) {
fstream File("/Users/zhang/Desktop/C++/fileIO/1.txt",ios::in | ios::app);
if (! File) {
cerr << "打开文件失败!" << endl;
return -1;
}
//写入文件
File << "hello world";
//将文件指针移动至文件开头
File.seekg(ios::beg);
//输入文件内容
char str[100];
File.getline(str, 100);
cout << str << endl;
File.close();
return 0;
}
文章来源:Devzhang