// writing on a text file #include<fstream.h>intmain(){
ofstream out(”out.txt”);if(out.is_open()){
out << ”This is a line.\n”;
out << ”This is another line.\n”;
out.close();}return0;}//结果: 在out.txt中写入:
This is a line.
This is another line
2.2 读文件示例
// reading a text file #include<iostream.h>#include<fstream.h>#include<stdlib.h>intmain(){
char buffer[256];
ifstream in(”test.txt”);if(! in.is_open()){
cout << ”Error opening file”;exit(