示例1.写文件
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
ofstream in; //如需自动创建文件,这里必须指明是ofstream
in.open("com.txt",ios::trunc); //ios::trunc表示在打开文件前将文件清空,由于是写入,文件不存在则创建
//ofstream in("com.txt",ios::trunc);也可以
//fstream in("com.txt",ios::trunc|ios::out);也行
int i;
char a='a';
for(i=1;i<=26;i++)//将26个数字及英文字母写入文件
{
if(i<10)
{
in<<"0"<<i<<"\t"<<a<<"\n";
a++;
}
else
{
in<<i<<"\t"<<a<<"\n";
a++;
}
}
in.close();//关闭文件
}
示例2.读文件
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char buffer[256];
fstream fmatrix;
fmatrix.open("fmatrix",ios::in);
//ifstreamfmatrix("fmatrix");也行
if (!fmatrix)
{
cout<<"unableto open fmatrix file!";
return -1;
}
while(!fmatrix.eof())
{
fmatrix.getline(buffer,256,'\n');
cout<<buffer<<endl;
}
fmatrix.close();
return 0;
}
fstream提供了三个类,用来实现c++对文件的操作:(文件的创建、读、写)
ifstream -- 从已有的文件读
ofstream -- 向文件写内容
fstream - 打开文件供读写
文件打开模式,似乎可以用“|”连接多个模式:
ios::in 读
ios::out 写
ios::app 从文件末尾开始写
ios::binary 二进制模式
ios::nocreate 打开一个文件时,如果文件不存在,不创建文件。
ios::noreplace 打开一个文件时,如果文件不存在,创建该文件
ios::trunc 打开一个文件,然后清空内容
ios::ate 打开一个文件时,将位置移动到文件尾
文件指针位置在c++中的用法:
ios::beg 文件头
ios::end 文件尾
ios::cur 当前位置
例子:
file.seekg(0,ios::beg); //让文件指针定位到文件开头
file.seekg(0,ios::end); //让文件指针定位到文件末尾
file.seekg(10,ios::cur); //让文件指针从当前位置向文件末方向移动10个字节
file.seekg(-10,ios::cur); //让文件指针从当前位置向文件开始方向移动10个字节
file.seekg(10,ios::beg); //让文件指针定位到离文件开头10个字节的位置
常用的错误判断方法:
good() 如果文件打开成功
bad() 打开文件时发生错误
eof() 到达文件尾