#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "fstream";
class Teacher {
public:
Teacher() {
this->age = 0;
strcpy(this->name, "");
}
Teacher(int age, char* name) {
this->age = age;
strcpy(this->name,name);
}
void printT() {
cout << age <<"+"<<name<< endl;
}
private:
int age;
char name[32];
};
int main() {
char *fname = "e:\\3.txt";
ofstream fout(fname, ios::binry);
Teacher t1(1, "bob");
Teacher t2(2, "alice");
fout.write((char *)&t1, sizeof(t1));
fout.write((char *)&t2, sizeof(t2));
fout.close();
ifstream fin(fname);
Teacher tmp;
fin.read((char *)&tmp, sizeof(tmp));
tmp.printT();
fin.read((char *)&tmp, sizeof(tmp));
tmp.printT();
fin.close();
system("pause");
return 0;
}
C++文件流:
fstream
ifstream
ofstream
#include<iomanip.h>
#include<fstream.h>
void main()
{
ofstream f1("d:\\me.txt");
if(!f1)return;
f1<<setw(20)<<"姓名:"<<"bob"<<endl;
f1<<setw(20)<<"家庭地址:"<<"sh"<<endl;
f1.close();
}
运行后打开文件d:\me.txt,其内容如下:
姓名:bob
家庭地址:sh
文件操作:
文件打开方式选项:
ios::in = 0x01,
ios::out = 0x02,
ios::ate = 0x04,
ios::app = 0x08,
ios::trunc = 0x10,
ios::nocreate = 0x20,
ios::noreplace = 0x40,
ios::binary = 0x80
文件保护方式选择项:
filebuf::openprot;
filebuf::sh_none;
filebuf::sh_read;
filebuf::sh_write;
打开文件的方法
调用构造函数时指定文件名和打开模式
ifstream f("d:\\12.txt",ios::nocreate);
ofstream f("d:\\12.txt");
fstream f("d:\\12.dat",ios::in|ios::out|ios::binary);
使用Open成员函数
fstream f;
f.open("d:\\12.txt",ios::out);
检查是否成功打开
成功:
if(f){...}
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<fstream.h>
void main()
{
fstream f("d:\\try.txt",ios::out);
f<<1234<<' '<<3.14<<'A'<<"How are you";
f.close();
f.open("d:\\try.txt",ios::in);
int i;
double d;
char c;
char s[20];
f>>i>>d>>c;
f.getline(s,20);
cout<<i<<endl;
cout<<d<<endl;
cout<<c<<endl;
cout<<s<<endl;
f.close();
}
运行结果:
1234
3.14
A
How are you
Press any key to continue
显示文本文件的内容
#include<fstream.h>
void main()
{
ifstream fin("d:\\简介.txt",ios::nocreate);
if(!fin){
cout<<"File open error!\n";
return;
}
char c;
while((c=fin.get())!=EOF)cout<<c;
fin.close();
}
#include<fstream.h>
void main()
{
ifstream fin("d:\\简介.txt",ios::nocreate);
if(!fin){
cout<<"File open error!\n";
return;
}
char c[80];
while(fin.get(c,80,'\0')!=NULL)cout<<c;
fin.close();
}
#include<fstream.h>
void main()
{
ifstream fin("d:\\简介.txt",ios::nocreate);
if(!fin){
cout<<"File open error!\n";
return;
}
char c[80];
while(!fin.eof())
{
fin.read(c,80);
cout.write(c,fin.gcount());
}
fin.close();
}
拷贝文件
#include<fstream.h>
void main()
{
ifstream fin("C:\\1.exe",ios::nocreate|ios::binary);
if(!fin){
cout<<"File open error!\n";
return;
}
ofstream fout("C:\\2.exe",ios::binary);
char c[1024];
while(!fin.eof())
{
fin.read(c,1024);
fout.write(c,fin.gcount());
}
fin.close();
fout.close();
cout<<"Copy over!\n";
}