许多C++程序会和文件关联,下面简单说说C++文件I\O的一些基本知识点:
首先,要写入文件,即向文件中写入东西,需要一个ofstream类的对象,它是ostream的派生类。
同样,要读取文件中的东西,需要一个ifstream类的对象,它是istream的派生类。
因为是iostram的派生类,他们可以使用iostream的方法。
ofstream和ifstream都包含于fstream头文件中。一般地,fstream头文件包括了iostream头文件,所以可以不用显示地包含iostream头文件。
让程序写入文件:
1、创建一个ofstream对象来管理输出流;
2、将这个ofstream对象与目标文件关联起来。
3、以使用cout的方式来使用该对象,唯一不同的是,输出将进入文件,而不是屏幕;
需要指出的是,关联的文件默认在程序所在的目录里。
举个栗子:
#include "fstream"
using namespace std;
int main() {
ofstream fout; //创建一个ofstream对象
fout.open("test.txt"); //将该对象和目标文件关联,如果没有该文件,则创建之
//也可以这么做:ofstream fout("test.txt");将以上两条句子合并
fout << "This is a test file."; //像使用cout一样使用它。
return 0;
}
用这种默认的方法打开文件并写入,会在写入之前自动删除该文件原有内容。
读取文件:
1、创建一个ifstream对象来管理输入流;
2、将该对象与特定的文件关联起来;
3、以使用cin的方式使用该对象;
举个栗子:
#include "fstream"
using namespace std;
int main() {
ifstream fin;
fin.open("test.txt");
//也可以这么做:ifstream fin("test.txt");
char ch;
fin >> ch;
char buff[80];
fin >> buff;
fin.getline(buff, 80);
string str;
getline(fin, str);
return 0;
}
另外,可以用fout.close()来显示地关闭fout到文件的连接。
下面,我们来看一个简短的例子:
该程序要求输出文件名,然后创建一个名称为输出名的文件,将一些信息写入到该文件中,然后关闭该文件。关闭文件将刷新缓冲区,从而确保文件被更新。
然后,程序打开该文件,读取并显示其内容。
#include "iostream"
#include "fstream"
#include "string"
int main() {
using namespace std;
string filename;
cout << "Enter name for new file: ";
cin >> filename;
ofstream fout(filename.c_str());
fout << "For your eyes only!\n";
cout << "Enter your secret number: ";
float secret;
cin >> secret;
fout << "Your secret number is " << secret << endl;
fout.close();
ifstream fin(filename.c_str());
cout << "Here are the contents of " << filename << ":\n";
char ch;
while (fin.get(ch)) //get函数会一直读取直到遇到文件末尾,即EOF
cout << ch;
cout << "Done\n";
fin.close();
return 0;
}
下面是程序运行结果:
另:可以用is_open()成员函数来检测打开文件是否成功。如:
if (!(fin.is_open()) {
...
}
文件模式:
以默认的方式打开文件时会删除掉文件原有的内容(如果有的话),你可能会抱怨这种设定。那末,可能用别的模式打开吗?答案是肯定的。
表:文件模式常量:
常量 | 含义 |
---|---|
ios_base::in | 打开文件,以便读取 |
ios_base::out | 打开文件,以便写入 |
ios_base::ate | 打开文件并移到文件尾 |
ios_base::app | 追加到文件尾 |
ios_base::trunc | 如果文件存在,则截短文件 |
ios_base::binary | 二进制文件 |
上述是ifstream类和ofstream类的构造函数和open成员函数的第二个参数。
当我们调用open函数和构造函数时只传入一个参数时,第二个参数将使用其默认值。
ifstream的open函数和构造函数的第二个参数的默认值是ios_base::in(打开文件以读取)
而ofstream的open函数和构造函数的第二个参数的默认值是ios_base::out | ios_base::trunc(打开文件并截短以写入)
注:位运算符在这里用于将两个文件模式常量结合。
如果要保留文件内容,并在文件末尾追加新信息,则可以:
ofstream fout(filename, ios_base::out | ios_base::app);
注意ios_base::out本身与ios_base::out | ios_base::trunc效果相同。
ios_base::out | ios_base::in (打开以读写,不会导致文件被截短)
下面是一个文件追加的简单例子:
//append.cpp:
#include "iostream"
#include "fstream"
#include "string"
#include "cstdlib"
using namespace std;
const char* file = "guests.txt";
int main() {
char ch;
//show initial contents
ifstream fin;
fin.open(file);
if (fin.is_open()) { //determine if it is opened succeed
cout << "Here are the current contents of the " << file << " file:\n";
while (fin.get(ch))
cout << ch;
fin.close();
}
//add new names
ofstream fout(file, ios::out | ios::app); //ios_base::out可以简化成ios::out
if (!fout.is_open()) {
cerr << "Can't open " << file << " file for output.\n";
exit(EXIT_FAILURE);
}
cout << "Enter guest names (enter a blank line to qiut):\n";
string name;
while (getline(cin, name) && name.size() > 0) {
fout << name << endl;
}
fout.close();
//show revised file
fin.clear();
fin.open(file);
if (fin.is_open()) {
cout << "Here are the new contents of the "
<< file << " file:\n";
while (fin.get(ch))
cout << ch;
fin.close();
}
cout << "Done!\n";
return 0;
}
第一次运行结果:
第二次运行结果:
修改文件信息
你可能想知道有没有一种方法能够修改文件中任何一个地方的信息而不是只能追加信息。
C++中seekp()和seekg()函数功能
seekp:设置输出文件流的文件流指针位置
seekg:设置输入文件流的文件流指针位置
函数原型:
ostream& seekp( streampos pos );
ostream& seekp( streamoff off, ios::seek_dir dir );
istream& seekg( streampos pos );
istream& seekg( streamoff off, ios::seek_dir dir );
第一个原型定位到离文件开头特定的距离(单位为字节)的位置。
第二个原型定位到离第二个参数指定的文件位置特定距离(单位为字节)的位置。
关于字节的说明:
ASCII码中,一个英文字母(不分大小写)占一个字节的空间,一个中文汉字占两个字节的空间。
其中第二个参数可选的为:
ios::beg:文件流的起始位置
ios::cur:文件流的当前位置
ios::end:文件流的结束位置
将其设置为要修改的位置后,就可以修改之,当然对应的文件打开方式是fout.open(filename, ios::out | ios::in);
另外,tellp()和tellg()函数返回当前位置对起始处的偏移量。两个函数分别对应输出文件流和输入文件流。
如:
#include "fstream"
#include "string"
#include "iostream"
using namespace std;
int main() {
ofstream fout;
fout.open("test.txt");
fout << "This is a test file.";
fout.close();
fout.open("test.txt", ios::out | ios::in);
fout.seekp(-6, ios::end);
fout << "app";
cout << fout.tellp() << endl; //输出为17
return 0;
}