当前位置: 首页 > news >正文

IO操作(Num22)

目录

一、流的概念:

二、 fstream的使用:

打开方式:

 代码示例1:

代码示例2:

三、文件打包:

代码示例:


一、流的概念:

流是一种抽象的概念,表示了数据的无结构化传递,由一端流向另一端常见的:

  1. 输入/输出流(I/O流):键盘向程序内部输入,程序内部向屏幕输出

  2. 数据流:通常指网络中的数据传递

  3. 文件流:通常指读写文件

二、 fstream的使用:

fstream是C++中常用的文件操作类,用于文件操作,和C语言学过的文件操作作用一样
使用:

  1. 包含头文件 fstream,并打开命名空间std 或是std::fstream;

  2. 使用fstream类来实例化对象,通过对象的成员来进行文件操作 

打开方式:

open(文件路径,打开方式)

  1. 文件路径:绝对路径或相对路径

  2. 打开方式:表示打开文件的模式,不同的模式对应不同的数据操作(只读,只写,二进制操作等)

  • ios::in                 以只读方式打开文件(默认方式)

  • ios::out                 以写入方式打开文件,如果文件不存在则创建新文件

  • ios::app                以追加方式打开文件,在文件末尾追加写入内容

  • ios::ate                 打开文件后定位到文件末尾,可以随机访问并修改文件内容

  • ios::trunc              如果文件存在,则先删除文件内容,再以写入方式打开文件

  • ios::binary            以二进制方式打开文件,用于处理二进制文件

  • ios::sin | ios::put   以读写方式打开文件,在文件的任意位置进行读写操作 

 代码示例1:

#include<iostream>
#include<fstream>using namespace std;int main() {fstream file;file.open("1.txt",ios::in|ios::out);//cout<<file.is_open();//判断文件是否打开了//file.put('c');if (!file.is_open()) {cout << "文件打开失败" << endl;}file.seekp(0,ios::end);//文件最末尾int size = file.tellp();file.seekp(0, ios::beg);//文件最末尾int z = 0;while (!file.eof()) {//判断文件指针是否到了末尾char c[100] = { 0 };file.getline(c, 99);z += 2;cout << "读取进度:"<<size<<"/"<<z<<endl;}cout << "读取进度:" << size << "/" << size << endl;cout<<file.tellp();char c[100] = {0};//file.get(c,99);//只能读一行,\n不会读换行//file.get();//file.get(c, 99,'r');//cout << c;file.getline(c, 99);//读取一行换行也读进来cout << c;file.getline(c, 99);cout << c;file.close();return 0;
}

代码示例2:

#include<iostream>
#include<fstream>using namespace std;int main() {fstream file;file.open("1.txt", ios::in | ios::out);if (!file.is_open()) {cout << "文件打开失败"<<endl;}int wage = 18;char wname[] = "XXX";file << wage << "\t" << wname << endl;file << 20 << "\t" << "XXXXX" << endl;file.seekp(0, ios::beg);int age = 0;char str[10] = { 0 };file >> age >> str;cout << age << "   " << str<<endl;file >> age >> str;cout << age << "   " << str<<endl;file >> age >> str;cout << age << "   " << str<<endl;//int wid = 100;//file.write((const char*)&wage, sizeof(int));//int namesize = strlen(wname);//file.write((const char*)&namesize, sizeof(int));//file.write(wname, strlen(wname));//file.write((const char*)&wid, sizeof(int));//file.seekp(0, ios::beg);//int rage = 0;//char rname[10] = { 0 };//int rid = 0;//int x;//file.read((char*)&rage, sizeof(int));//file.read((char*)&x, sizeof(int));//file.read(rname, 4);//这里不能写数组的长度应该为读取内容的长度//file.read((char*)&rid, sizeof(int));//cout << rage << " " << rname << " " << rid << endl;/*char str[] = "这是一行字符串";file.write(str,strlen(str));*///int x = 10;//file.write((const char*)&x,sizeof(int));//file.seekp(0, ios::beg);//int y = 0;//file.read((char*)&y, sizeof(int));//cout << y << endl;file.close();return 0;
}

三、文件打包:

把多个文件以某种格式写入到文件中,让别人不能随意获取使用

代码示例:

#include<iostream>
#include<fstream>
using namespace std;struct node
{int filesize;int namesize;char name[20];
};void dabao() {node n[6] = {{0,5,"1.jpg"},{0,5,"2.jpg"},{0,5,"3.jpg"},{0,5,"4.jpg"},{0,5,"5.jpg"},{0,5,"6.jpg"}};fstream file[6];for (int i = 0; i < 6; i++) {file[i].open(n[i].name, ios::in);file[i].seekg(0, ios::end);n[i].filesize = file[i].tellg();file[i].seekg(0,ios::beg);}fstream outfile("1.pack", ios::out);int size = 6;for (int i = 0; i < 6; i++) {outfile.write((char*)&n[i].filesize, sizeof(int));outfile.write((char*)&n[i].namesize, sizeof(int));outfile.write(n[i].name, n[i].namesize);}for (int i = 0; i < 6; i++) {while (!file[i].eof()){outfile.put(file[i].get());}file[i].close();}outfile.close();
}void chaibao() {fstream file("1.pack", ios::binary);int size = 0;file.read((char*)&size,sizeof(int));node* n = new node[size];memset(n, 0, sizeof(node) * size);fstream* outfile = new fstream[size];for (int i = 0; i < size; i++) {file.read((char*)&n[i].filesize, sizeof(int));file.read((char*)&n[i].namesize, sizeof(int));file.read(n[i].name, n[i].namesize);outfile[i].open(n[i].name, ios::out | ios::binary);for (int i = 0; i < 6; i++) {for (int j = 0; j <= n[i].filesize; j++) {outfile[i].put(file.get());}outfile[i].close();}file.close();delete[] n;delete[] outfile;}
}int main() {dabao();chaibao();return 0;
}
http://www.dtcms.com/a/461460.html

相关文章:

  • 领码方案|微服务与SOA的世纪对话(6):组织跃迁——智能架构下的团队与文化变革
  • 怎么什么软件可以吧做网站网站被百度收录很重要
  • C++ 单例模式(Singleton)详解
  • 面向未来的数据平台
  • C++5d
  • Transformer实战(21)——文本表示(Text Representation)
  • 网站空间商 权限梵克雅宝
  • 【Vue 3 】——setup、ref、watch
  • 做期货网站违法的吗淄博市住房和城乡建设局网站
  • 使用feign进行远程调用出现的问题(文件服务参数接收为null)
  • 国自然·医工交叉热点|通用医学影像分割基础模型与数据库
  • React Native:关于react自定义css属性的位置
  • 对于el-table中自定义表头中添加el-popover会弹出两个的解决方案,分别针对固定列和非固定列来隐藏最后一个浮框。
  • 电子商务公司简介系统清理优化工具
  • 内网渗透实战:红队作战全解析
  • Verilog和FPGA的自学笔记4——多路选择器1(always语句)
  • 前端架构师,是架构什么
  • Coze源码分析-资源库-编辑数据库-后端源码-安全与错误处理
  • 制作专业网站餐厅网络推广方案
  • 掌握MyBatis Java API:高效操作数据库
  • 搭建网站 程序招工网站怎么做
  • 数据库设计_理论部分_设计方法设计过程
  • 【三维重建-算法解析】MVS(Multi-View Stereo,多视图立体)
  • 【GPT5系列】ChatGPT5 提示词工程指南
  • 61850协议GOOSE通信AB网通信
  • wordpress开启子站找公司做网站有什么好处
  • SpringBoot+Redis实现电商秒杀方案
  • 电子商务网站模板 html数据型网站
  • 【QT常用技术讲解】QSerialPort串口开发,包含文件发送功能
  • STM32 外设驱动模块【含代码】:SG90 舵机模块