例如:以二进制输入方式打开文件c:config.sys
fstream file1;
file1.open("c:\config.sys",ios::binary|ios::in,0);
如果open函数只有文件名一个参数,则是以读/写普通文件打开,即:
file1.open("c:\config.sys");<=>file1.open("c:\config.sys",ios::in|ios::out,0);
二、关闭文件
打开的文件使用完成后一定要关闭,fstream提供了成员函数close()来完成此操作。
三、读写文件
int i;
file1>>i;//从文件输入一个整数值。
这种方式还有一种简单的格式化能力,比如可以指定输出为16进制等等,具体的格式有以下一些
操纵符 功能 输入/输出
dec 格式化为十进制数值数据 输入和输出
endl 输出一个换行符并刷新此流 输出
ends 输出一个空字符 输出
hex 格式化为十六进制数值数据 输入和输出
oct 格式化为八进制数值数据 输入和输出
setpxecision(int p) 设置浮点数的精度位数 输出
#include
#include
#include
int main ()
{
std::ofstream out("hao.txt");
if (out.is_open())
{
out <
2、二进制文件的读写
①put()函数向流写入一个字符,其原型是ofstream &put(char ch),如file1.put('c');就是向流写一个字符'c'。
②get(),有3种常用的重载形式:
ifstream &get(char &ch);功能是从流中读取一个字符,结果保存在引用ch中,如果到文件尾,返回空字符。
int get();这种形式是从流中返回一个字符,如果到达文件尾,返回EOF,如x=file2.get()。
还有一种形式的原型是:ifstream &get(char *buf,int num,char delim=' ');这种形式把字符读入由 buf 指向的数组,直到读入了 num 个字符或遇到了由 delim 指定的字符,如果没使用 delim 这个参数,将使用缺省值换行符' '。例如:
③读写数据块
要读写二进制数据块,使用成员函数read()和write()成员函数,它们原型如下:
read(unsigned char *buf,int num);
write(const unsigned char *buf,int num);
read()从文件中读取 num 个字符到 buf 指向的缓存中,如果在还未读入 num 个字符时就到了文件尾,可以用成员函数 int gcount();来取得实际读取的字符数;而 write() 从buf 指向的缓存写 num 个字符到文件中,值得注意的是缓存的类型是 unsigned char *,有时可能需要类型转换。
unsigned char str1[]="I Love You";
int n[5];
ifstream in("xxx.xxx");
ofstream out("yyy.yyy");
out.write(str1,strlen(str1));//把字符串str1全部写到yyy.yyy中
in.read((unsigned char*)n,sizeof(n));//从xxx.xxx中读取指定个整数,注意类型转换
in.close();out.close();
例子
文件保护方式选择项:
filebuf::openprot; //默认的兼容共享方式
filebuf::sh_none; //独占,不共享
filebuf::sh_read; //读共享
filebuf::sh_write; //写共享
检查是否成功打开
成功:
if(f){...} //对ifstream、ofstream对象可用,fstream对象不可用。
if(f.good()){...}
失败:
if(!f){...} // !运算符已经重载
if(f.fail()){...}
读写操作
使用<<,>>运算符,只能进行文本文件的读写操作,用于二进制文件可能会产生错误。
使用函数成员 get、put、read、write等
经常和read配合使用的函数是gcount(),用来获得实际读取的字节数。
读写二进制文件注意事项
打开方式中必须指定ios::binary,否则读写会出错
用read\write进行读写操作,而不能使用插入、提取运算符进行操作,否则会出错。
使用eof()函数检测文件是否读结束,使用gcount()获得实际读取的字节数
关闭文件
使用成员函数close,如:
f.close();
利用析构函数:对象生命期结束时会检查文件是否关闭,对没有关闭的文件进行关闭操作。
随机读写文件
通过移动文件读写指针,可在文件指定位置进行读写。
seekg(绝对位置); //绝对移动, //输入流操作
seekg(相对位置,参照位置); //相对操作
tellg(); //返回当前指针位置
seekp(绝对位置); //绝对移动, //输出流操作
seekp(相对位置,参照位置); //相对操作
tellp(); //返回当前指针位置
参照位置:
ios::beg = 0 //相对于文件头
ios::cur = 1 //相对于当前位置
ios::end = 2 //相对于文件尾
读写文本文件的示例
//为能够正确读出写入文件的各数据,各数据间最好要有分隔
#include
#include
int main()
{
std::fstream f("ok.txt",std::ios::out);
f<<1234<<' '<<3.14<<'A'<<"How are you"; //写入数据
f.close();
f.open("ok.txt",std::ios::in);
int i;
double d;
char c;
char s[20];
f>>i>>d>>c; //读取数据
f.getline(s,20);
std::cout<
运行结果:
1234
3.14
A
How are you
#include
#include
int main()
{
//std::ifstream fin("d:\\简介.txt"); //输出:File open error!
std::ifstream fin("ok.txt"); //输出:1234 3.14AHow are you
if(!fin)
{
std::cout<<"File open error!\n";
return 0;
}
char c;
while((c=fin.get())!= EOF) //注意结束条件的判断 # define EOF (-1)
{
std::cout<
//使用get(char *,int n,char delim='\n')一次读多个字符----方案二
//巧妙利用文本文件中不会有字符'\0'的特点进行读取
#include
#include
int main()
{
std::ifstream fin("ok.txt"); //输出:1234 3.14AHow are you
if(!fin)
{
std::cout<<"File open error!\n";
return 0;
}
char c[80];
while(fin.get(c,80,'\0')!=NULL)//注意结束条件的判断
{
std::cout<
//使用read(char *,int n)读文件---------------------------方案三
#include
#include
int main()
{
std::ifstream fin("ok.txt"); //输出:1234 3.14AHow are you
if(!fin)
{
std::cout<<"File open error!\n";
return 0;
}
char c[80];
while(!fin.eof()) //判断文件是否读结束
{
fin.read(c,80);
std::cout.write(c,fin.gcount());
}
fin.close();
return 0;
}
拷贝文件
//二进制文件操作示例
#include
#include
int main()
{
std::ifstream fin("ok.txt",std::ios::binary);
if(!fin)
{
std::cout<<"File open error!\n";
return 0;
}
std::ofstream fout("ok1.txt",std::ios::binary);
char c[1024];
while(!fin.eof())
{
fin.read(c,1024);
fout.write(c, fin.gcount());
}
fin.close();
fout.close();
}