C++之文件操作

c++中文件操作头文件:fstream
文件类型:文件文件和二进制文件
文件操作三大类:
ofstream 写操作
ifstream 读操作
fstream:读写操作
文件打开方式:
标志 说明
ios::in  只读
ios::out 只写,文件不存在则创建,存在则打开并截断原内容
ios::ate  打开一个已有的文件,并指向文件读指针指向文件尾,若文件不存在,则打开出错
ios::app 打开文件,从文件尾添加内容,若文件不存在则创建
ios::trunc 打开文件同时会截断原内容,单独使用时与ios::out相同
ios::binary 以二进制方式打开
ios::in|ios::out  打开文件,可读也可写,文件打开时原内容保持不变,若不存在则打开出错
ios::in|ios::out|ios::trunc  打开文件,可读写,会截断原内容,文件不存在则创建
1.文本方式写入示例 #include #include using namespace std;int main(){ /*1.创建文件*/ ofstream fp; fp.open(test.txt,ios::out);//创建文件,会截断原内容 if (!fp.is_open())//文件打开失败返回false { cout < < 文件打开失败! < < endl; return 0; } fp < < c++文件操作示例! < < endl; fp < < 写入数据测试 < < endl; fp < < 姓名:it_阿水 < < t工作方向: < < 嵌入式开发 < < t工作时间: < < 6年 < >读取#include #include using namespace std;int main(){ ifstream ifs; ifs.open(test.txt,ios::in);//只读方式打开 if (!ifs.is_open()) { cout < < 文件打开失败! str)//以字符串方式读取 { cout < < str= < < str < < endl;; } //关闭文件 ifs.close(); system(pause);}  2.2 利用成员函数getline读取#include #include using namespace std;int main(){ ifstream ifs; ifs.open(test.txt,ios::in);//只读方式打开 if (!ifs.is_open()) { cout < < 文件打开失败! < < endl; return 0; } //第二种:getline() char buff[1024]; while (ifs.getline(buff, sizeof(buff))) { cout < < buff= < < buff < < endl; } //关闭文件 ifs.close(); system(pause);} 2.3 单个字符方式读取get()#include #include using namespace std;int main(){ ifstream ifs; ifs.open(test.txt,ios::in);//只读方式打开 if (!ifs.is_open()) { cout < < 文件打开失败! < < endl; return 0; } //第三种:单个字符方式读取 char c; while ((c = ifs.get()) != eof) { cout < name, name); this- >age = age; } char name[20];//姓名 int age;//年龄};int main(){ /*二进制写入数据示例*/ fstream fs(test.doc, ios::out | ios::binary); if (!fs.is_open()) { cout < < 文件创建失败 < < endl; return 0; } person p(小王, 18); fs.write((const char *) & p, sizeof(p));//写入内容 fs.close();//关闭文件 /*二进制读取数据示例*/ fs.open(test.doc, ios::in | ios::binary); if (!fs.is_open()) { cout < < 文件打开失败 < < endl; return 0; } person p2; fs.read((char *) & p2, sizeof(p2)); cout < < 读取的内容: < < endl; cout < < 姓名: < < p2.name < < t年龄: < < p2.age < < endl; fs.close(); system(pause);} 4.c++指针偏移c++文件指针偏移 seekg(pos_type _pos,ios_base::seekdir _way) --用于输入流,偏移位置指针到指定位置 seekp(pos_type _pos,ios_base::seekdir _way) --用于输出流,偏移位置指针到指定位置 第一个参数:偏移量 第二个参数:基于哪个位置 ios::beg --文件头 ios::end --文件尾 ios::cur --当前位置 streamoff tellg() --用于输入流,返回当前指针位置,streamoff 是一个long long类型 streamoff tellp() --用于输出流,返回当前指针位置 返回值返回基于文件头的偏移量,字节为单位。失败则返回-1 示例:
#include #include using namespace std;int main(){ ifstream fs; fs.open(test.txt, ios::in );//打开文件,不存在则打开失败,不会截断原内容 if (!fs.is_open()) { cout < < 文件打开失败 < < endl; return 0; } fs.seekg(0,ios::end);//将文件指针偏移到文件末尾 char buff[1024]; streamoff size = fs.tellg();//获取文件大小 cout < < 文件大小: < < size < < 字节 buff) { cout < < buff < < endl; } fs.close(); system(pause); return 0;} 5.c++中使用fopen系列函数示例#define _crt_secure_no_deprecate#include using namespace std;int main(){ cout < < 写入数据示例: < < endl; string file = 1.txt; file* fp = fopen(file.c_str(),w+b); if (fp == null) { cout < < 创建文件失败 < < endl; } char buff[] = c语言格式文件读写操作示例!; size_t size=fwrite(buff,1,sizeof(buff),fp); cout < < 文件写入成功!size= < < size < < endl; fclose(fp); cout < < 读取数据示例: < < endl; fp = fopen(file.c_str(), rb); if (fp == null) { cout < < 打开文件失败 < < endl; } char data[200]; size=fread(data, 1, sizeof(data), fp); data[size] = '�'; cout < < 读取内容: < < data < < t字节数: < < size < < endl; fclose(fp); system(pause);}

这么多集成型 LCD 的 MSP430产品,哪一款才真正适合您?
基于FPGA+PC104实现新型防碰撞报警系统的数据处理功能
供应链深度分析 | 苹果MR Vision Pro将会带动哪些零部件出货?
雷诺公布一款机器人概念车 可为用户提供高端的出行体验
聊聊冬天对锂电池的影响
C++之文件操作
充分利用你的PicoBlaze微处理器
以“光”之名,传递未来|奥林巴斯携硬核产品亮相第23届光博会
2017年我国调整集成电路等进出口关税
雷军做了件“蠢事”!小米红米note4x机这样弄 在“左手打右手”?
跨机房ES同步实战
不惧严寒 超威电池质量受消费者盛赞
助力方案商海内外应用落地,参考方案分享
EPR6-S工业机器人通过EtherCAT转profinet网关接入西门子系统
深度解析大语言模型内部运行原理
武汉新芯能拯救国产存储产业?没那么简单!
锂电池和铅酸电池有什么区别?哪个好?
普通汽车改造成无人驾驶汽车需要几步?看完你就会了
国内知名比特币交易网站有哪些?
走进上海康桥 ABB“超级工厂”