七、C++ 文件操作
来源:互联网 发布:linux打开摄像头命令 编辑:程序博客网 时间:2024/05/17 07:49
C++ 文件操作
一、基本概念
数据在计算机中保存的单位是位(bit),8位组成一个字节(byte),若干个个字节组成一个纪录/域,比如学生记录:
int ID;char name[10];int age;int rank[10];
将所有记录顺序地写入一个文件,生成的文件就叫顺序文件。C++标准库中,共有三个类用于文件操作,分别是ifstream、ofstream、fstream,它们统称为文件流类。
二、建立顺序文件
文件操作的基本流程是:1) 打开文件,2) 读/写文件,3) 关闭文件。
建立顺序文件的代码:
#include <fstream> // 包含头文件ofstream outFile("clients.dat", ios::out|ios::binary); //打开文件
- ofstream是fstream中定义的类,outFile是ofstream对象
- “clients.dat”是将要建立的文件的文件名
- ios::out指输出到文件时,删除原来内容;如果换为ios::app,则是指输出到文件时,保留原有内容,在文件尾部添加数据
- ios::binary,指以二进制文件格式打开文件
也可以先创建 ofstream对象, 再用 open函数 打开
ofstream fout;fout.open( “test.out”, ios::out|ios::binary );
判断打开是否成功:
if(!fout) { cerr << “File open error!”<<endl; }
文件名可以给出绝对路径,也可以给相对路径;没有交代路径信息, 就是在当前文件夹下找文件。
三、文件的读写指针
ofstream fout(“a1.out”, ios::app);long location = fout.tellp(); //取得写指针的位置location = 10L;fout.seekp(location); // 将写指针移动到第10个字节处fout.seekp(location, ios::beg); //从头数locationfout.seekp(location, ios::cur); //从当前位置数locationfout.seekp(location, ios::end); //从尾部数locationifstream fin(“a1.in”,ios::in);long location = fin.tellg(); //取得读指针的位置location = 10L;fin.seekg(location); //将读指针移动到第10个字节处fin.seekg(location, ios::beg); //从头数locationfin.seekg(location, ios::cur); //从当前位置数locationfin.seekg(location, ios::end); //从尾部数location//location 可以为负值
四、二进制文件读写
本次程序示例中,读写的数据为Student对象。
class Student{ public: char name[20]; int age;};
4.1 写文件
void writeFile(){ Student s; ofstream outfile("student.dat", ios::out|ios::binary); while(cin>>s.name>>s.age){ if(stricmp(s.name, "exit") == 0) break; outfile.write((char *)&s, sizeof(s)); } outfile.close();}
4.2 读文件
void readFile(){ Student s; ifstream infile; infile.open("student.dat", ios::in|ios::binary); if(!infile){ cout<<"error"<<endl; return; } while(infile.read( (char *)&s, sizeof(s) )){ int readBytes = infile.gcount(); cout<<s.name<<" "<<s.age<<endl; } infile.close();}
4.3 更新文件
void updateFile(){ Student s; fstream iofile; iofile.open("student.dat", ios::in|ios::out|ios::binary); if(!iofile){ cout<<"error"<<endl; return; } iofile.seekp(2*sizeof(s), ios::beg); iofile.write("steven", strlen("steven")+1); iofile.seekg(-3*sizeof(s), ios::end); while(iofile.read( (char *)&s, sizeof(s) )){ int readBytes = iofile.gcount(); cout<<s.name<<" "<<s.age<<endl; } iofile.close();}
4.4 复制文件
int main(int argc, char* argv[]){ if(argc!= 3){ cout<<"file name error"<<endl; return 0; } ifstream infile(argv[1], ios::in|ios::binary); if(!infile){ cout<<"error"<<endl; return 0; } ofstream outfile(argv[2], ios::out|ios::binary); if(!outfile){ cout<<"error"<<endl; infile.close(); //打开的文件一定要记得关闭 return 0; } char c; while(infile.get(c)) outfile.put(c); infile.close(); outfile.close(); return 0;}
五、小结
文件的读写、修改、复制本质上都差不多,基本步骤都是1) 打开文件、2) 判断打开是否成功、3) 写文件/读文件、4)关闭文件。关键代码如下:
ofstream outfile("student.dat", ios::out|ios::binary); (或ifstream infile, ios::in) //打开文件iofile.seekp(2*sizeof(s), ios::beg); (或iofile.seekg) //文件读写指针定位outfile.write((char *)&s, sizeof(s)); (或infile.read) //读/写文件iofile.close(); //关闭文件
0 0
- C语言系列(七)文件的相关操作
- 七、C++ 文件操作
- 趣学 C 语言(七)—— 文件读写操作
- C 语言学习笔记(七)——文件操作(2)
- [C/C++]文件操作
- Java文件操作大全(七)
- JAVA学习(七) JAR文件操作
- 七、android文件读写操作 总结
- python进阶七_文件操作(一)
- python进阶七_文件操作(二)
- python进阶七_文件操作(三)
- linux学习之七---文件操作
- 文件操作--C 函数操作
- 【C解析之七】文件进阶
- 【C解析之七】文件进阶
- c文件操作
- C文件操作
- C的文件操作
- 极光推送
- ALSA中PCM参数配置
- json调用
- XHTML 与 HTML 之间的差异
- eclipse插件
- 七、C++ 文件操作
- 学习javaweb过程中遇到的一些小error
- 单例
- Android之intent传值的三种方法
- Find Minimum in Rotated Sorted Array
- _(:зゝ∠)_
- 创建私有cocoapods repo库 —— Private Pods
- 【开车了】9个大数据竞赛思路分享
- Java泛型简介