目录
一、基本概念
二、文本文件的读写
1. 文本文件的写文件
2. 文本文件的读文件
三、二进制文件的读写
1. 二进制文件的写文件
2. 二进制文件的读文件
1. 文件类型
文本文件:文件以文本的ASCII码形式存储在计算机中二进制文件:文件以文本的二进制形式存储在计算机中2. 文件操作
头文件:#include<fstream>操作文件的三大类:ofstream(写操作:out file stream)、ifstream(读操作:in file stream)、fstream(读写操作)3. 文件打开方式
模式标记适用对象作用ios::inifstream fstream打开文件用于读取数据。如果文件不存在,则打开出错。ios::outofstream fstream打开文件用于写入数据。如果文件不存在,则新建该文件;如果文件原来就存在,则打开时清除原来的内容。ios::appofstream fstream打开文件,用于在其尾部添加数据。如果文件不存在,则新建该文件。ios::ateifstream打开一个已有的文件,并将文件读指针指向文件末尾(读写指 的概念后面解释)。如果文件不存在,则打开出错。ios:: truncofstream打开文件时会清空内部存储的所有数据,单独使用时与 ios::out 相同。ios::binaryifstream ofstream fstream以二进制方式打开文件。若不指定此模式,则以文本模式打开。ios::in | ios::outfstream打开已存在的文件,既可读取其内容,也可向其写入数据。文件刚打开时,原有内容保持不变。如果文件不存在,则打开出错。ios::in | ios::outofstream打开已存在的文件,可以向其写入数据。文件刚打开时,原有内容保持不变。如果文件不存在,则打开出错。ios::in | ios::out | ios::truncfstream打开文件,既可读取其内容,也可向其写入数据。如果文件本来就存在,则打开时清除原来的内容;如果文件不存在,则新建该文件。注:文件打开方式可以配合使用,使用按位或符号"|"将其连接,例如以二进制打开文件并写入:ios::out | ios::binary
写文件步骤如下:
包含头文件:#include<fstream>创建流对象:ofstream 对象名;打开文件:对象名.open("文件名",打开方式);写数据:对象名<<"写入的数据";关闭文件:对象名.close(); #include <iostream> using namespace std; #include<fstream> //1. 包含头文件 void test() { //2. 创建流对象 ofstream ofs; //3. 打开文件 ofs.open("test.txt",ios::out); //4. 写数据 ofs << "姓名:张三" << endl; ofs << "性别:男" << endl; ofs << "年龄:28" << endl; //5. 关闭文件 ofs.close(); } int main() { test(); system("pause"); return 0; }读文件步骤如下:
包含头文件:#include<fstream>创建流对象:ifstream 对象名;打开文件并验证:对象名.open("文件名",打开方式); 使用is_open()判断是否打开成功读文件:四种读取方式关闭文件:对象名.close(); #include <iostream> using namespace std; #include<fstream> //1. 包含头文件 #include<string> void test() { //2. 创建流对象 ifstream ifs; //3.1 打开文件 ifs.open("test.txt",ios::in); //3.2 判断是否打开成功 if (!ifs.is_open()) { cout << "文件打开失败!" << endl; return; //文件打开失败,退出 } //4. 读数据 //4.1 使用C风格字符串接收数据,使用右移运算符读取数据 /* char buf[1024] = { 0 }; while (ifs >> buf) { cout << buf << endl; } */ //4.2 使用C风格字符串接收数据,使用ifstream的成员函数getline()读取数据 /* char buf[1024] = { 0 }; while (ifs.getline(buf,sizeof(buf))) { cout << buf << endl; } */ //4.3 使用C++风格字符串接收数据,使用全局函数getline()读取数据 /* string buf; while (getline(ifs, buf)) { cout << buf << endl; } */ //4.4 使用字符接收数据,使用ifstream的成员函数get()读取数据 char c; while ((c = ifs.get())!=EOF) { //EOF:即end of file,文件尾标志 cout << c; } //5. 关闭文件 ifs.close(); } int main() { test(); system("pause"); return 0; } 姓名:张三 性别:男 年龄:28 请按任意键继续. . .二进制文件操作时一般使用C风格字符串
二进制文件的写入主要利用流对象调用成员函数write实现。打开方式中要加入ios::binary
write原型:ofstream &write(const char *buffer, int len)
其中,字符指针buffer指向内存中一段存储空间,len是读写的字节数
#include <iostream> using namespace std; #include<fstream> //1. 包含头文件 class Student { public: char m_Name[64]; int m_Age; }; void test() { Student S = { "张三",18 }; //2. 创建流对象 ofstream ofs; //3. 打开文件 ofs.open("binary.txt", ios::out | ios::binary); //4. 写数据 ofs.write((const char *)&S, sizeof(Student)); //write()的第一个参数类型为const char *,因此将&S进行强制类型转换 //5. 关闭文件 ofs.close(); } int main() { test(); system("pause"); return 0; }二进制文件的读取主要利用流对象调用成员函数read实现。打开方式中要加入ios::binary
read原型:ofstream &read(har *buffer, int len)
其中,字符指针buffer指向内存中一段存储空间,len是读写的字节数
#include <iostream> using namespace std; #include<fstream> //1. 包含头文件 class Student { public: char m_Name[64]; int m_Age; }; void test() { Student S; //2. 创建流对象 ifstream ifs; //3. 打开文件 ifs.open("binary.txt", ios::in | ios::binary); //4. 写数据 ifs.read((char *)&S, sizeof(Student)); //read()的第一个参数类型为char *,因此将&S进行强制类型转换 cout << "姓名:" << S.m_Name << "\t年龄:" << S.m_Age << endl; //5. 关闭文件 ifs.close(); } int main() { test(); system("pause"); return 0; } 姓名:张三 年龄:18 请按任意键继续. . .