C++基础-8-文件读写操作
8. 文件读写操作
1 #include<iostream> 2 #include<string> 3 #include<fstream> // 读写文件 头文件 4 using namespace std; 5 6 // 文件操作 7 8 // 写文件 9 void writefile() { 10 11 ofstream ofs; 12 13 ofs.open("test.txt", ios::out); 14 15 ofs << "姓名:张三" << endl; 16 ofs << "性别:男" << endl; 17 ofs << "年龄:18" << endl; 18 19 ofs.close(); 20 21 } 22 23 // 读文件 24 void readfile() { 25 26 ifstream ifs; 27 28 // 打开文件,判断是否打开成功 29 ifs.open("test.txt", ios::in); 30 if (!ifs.is_open()) { 31 cout << "文件打开失败" << endl; 32 return; 33 } 34 35 // 读数据,4种方式 36 // 第一种 37 cout << "第一种读数据方式" << endl; 38 char buf1[1024] = { 0 }; 39 while ( ifs >> buf1 ) 40 { 41 cout << buf1 << endl; 42 } 43 44 // 第二种 45 cout << "第二种读数据方式" << endl; 46 char buf2[1024] = { 0 }; 47 while (ifs.getline(buf2, sizeof(buf2))) 48 { 49 cout << buf2 << endl; 50 } 51 52 // 第三种 53 string buf3; 54 cout << "第三种读数据方式" << endl; 55 while (getline(ifs, buf3)) 56 { 57 cout << buf3 << endl; 58 } 59 60 // 第四种 61 cout << "第四种读数据方式" << endl; 62 char c; 63 while ((c = ifs.get()) != EOF) // EOF: end of file 64 { 65 cout << c; 66 } 67 68 ifs.close(); 69 70 } 71 72 73 class Person { 74 public: 75 char m_Name[64]; 76 int m_Age; 77 }; 78 // 二进制文件-写操作 79 void writefile_binary() { 80 81 ofstream ofs("person.txt", ios::out | ios::binary); 82 83 //ofs.open("person.txt", ios::out | ios::binary) 84 85 Person p = { "张三", 18 }; 86 ofs.write((const char*)&p, sizeof(Person)); 87 88 ofs.close(); 89 } 90 91 // 二进制文件-读操作 92 void readfile_binary() { 93 94 ifstream ifs; 95 96 ifs.open("person.txt", ios::in | ios::binary); 97 if (!ifs.is_open()) { 98 cout << "文件打开失败" << endl; 99 return; 100 } 101 102 Person p; 103 ifs.read((char*)&p, sizeof(Person)); 104 cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl; 105 106 ifs.close(); 107 } 108 109 110 111 int main() { 112 113 //writefile(); 114 115 //readfile(); 116 117 //writefile_binary(); 118 119 readfile_binary(); 120 121 122 system("pause"); 123 124 return 0; 125 } 126 127 // 总结 128 // 对文件的操作,首先包含头文件<fstream> 129 // 130 // 文本文件:文件以ASCII形式存在在计算机中 131 // 二进制文件:以文本的二进制形式存储于计算机中 132 // 133 // 操作文件三类: 134 // ofstream: 写操作 135 // ifstream: 读操作 136 // fstream: 读写操作 137 // 138 // 读文件一般步骤: 139 // 1. 包含头文件 #include <fstream> 140 // 2. 创建流对象 ofstream ofs; 141 // 3. 打开文件 ofs.open("文件路径",打开方式) 142 // 4. 写数据 ofs << "写入数据"; 143 // 5. 关闭文件 ofs.close(); 144 // 145 // 写文件一般步骤: 146 // 1. 包含头文件 #include <fstream> 147 // 2. 创建流对象 ofstream ifs; 148 // 3. 打开文件 ifs.open("文件路径",打开方式) 149 // 4. 读数据:四种方式读取 150 // 5. 关闭文件 ifs.close(); 151 // 152 // 153 // 打开方式: 154 // ios::in 为读文件而打开文件 155 // ios::out 为写文件而打开文件 156 // ios::ate 初始位置:文件尾 157 // ios::app 追加方式写文件 158 // ios::trunc 如果文件存在,先删除,再创建 159 // ios::binary 二进制方式 160 // 注意:文件打开方式可以配合使用,利用 | 操作符 161 // 如:用二进制方式写文件 ios::binary | ios::out 162 // 163 // 164 // 对于二进制文件的读写操作 165 // 打开方式要指定为 ios::binary 166 // 二进制方式写文件主要利用对象调用成员函数write 167 // 函数原型: ostream& write(const char* buffer, int len); 168 // 参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数 169 // 170 // 二进制方式读文件主要利用对象调用成员函数read 171 // 函数原型:istream& read (char* buffer, int len); 172 // 参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数 173 //
参考《黑马程序员》C++教程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义