C++对于文件的相关操作 创建、读写、删除代码

创建

/*** @brief 创建文件:在密码设备内部创建用于存储用户数据的文件* @param pucFileName 缓冲区指针,用于存放输入的文件名,最大长度128字节* @param uiNameLen   文件名长度* @param uiFileSize  文件所占存储空间的长度*/void CreateFile(sdf_uint8_t *pucFileName, sdf_uint32_t uiNameLen,sdf_uint32_t uiFileSize) override {std::string file_name{reinterpret_cast<const char *>(pucFileName),uiNameLen};file_name = mgmt::get_md5_digest(file_name);int fd = open(file_name.c_str(),O_RDWR | O_CREAT );// TODO: error code, open errorif (fd == -1){ERR_EXIT("open error");}int ret = lseek(fd,uiFileSize,SEEK_CUR);// TODO: error code, leek errorif (ret == -1){ERR_EXIT("leek error");}close(fd);}

读入

/*** @brief 读取文件:读取密码设备内部存储的用户数据文件的内容* @param pucFileName 缓冲区指针,用于存放输入的文件名,最大长度128字节* @param uiNameLen 文件名长度* @param uiOffset 指定读取文件时的偏移值* @param puiReadLength  入参时指定读取文件内容的长度;出参时返回实际读取文件内容的长度* @param pucBuffer 缓冲区指针,用于存放读取的文件数据*/void ReadFile(sdf_uint8_t *pucFileName, sdf_uint32_t uiNameLen,sdf_uint32_t uiOffset, sdf_uint32_t *puiReadLength,sdf_uint8_t *pucBuffer) override {std::string file_name = reinterpret_cast<const char *>(pucFileName,uiNameLen);std::ifstream file(file_name.c_str(),std::istream::binary);file.seekg(uiOffset,std::ios::beg);file.read(reinterpret_cast<char *>(pucBuffer),reinterpret_cast<long>(puiReadLength));if (file)std::cout << "all characters read successfully.";else{std::cout << "error: only " << file.gcount() << " could be read";puiReadLength = reinterpret_cast<sdf_uint32_t *>(file.gcount());}file.close();}

写入

/*** @brief 写入文件:向密码设备内部存储用户数据的文件中写入内容* @param pucFileName 缓冲区指针,用于存放输入的文件名,最大长度128字节* @param uiNameLen 文件名长度* @param uiOffset 指定写入文件时的偏移值* @param uiWriteLength 指定写入文件内容的长度* @param pucBuffer 缓冲区指针,用于存放输入的写文件数据*/void WriteFile(sdf_uint8_t *pucFileName, sdf_uint32_t uiNameLen,sdf_uint32_t uiOffset, sdf_uint32_t uiWriteLength,sdf_uint8_t *pucBuffer) override {std::string file_name = reinterpret_cast<const char *>(pucFileName,uiNameLen);std::ofstream file(file_name.c_str(),std::istream::binary);file.seekp(uiOffset,std::ios::beg);file.write(reinterpret_cast<const char *>(pucBuffer),uiWriteLength);file.close();}

删除

/*** @brief 删除文件:删除指定文件名的密码设备内部存储用户数据的文件* @param pucFileName 缓冲区指针,用于存放输入的文件名,最大长度128字节* @param uiNameLen 文件名长度*/void DeleteFile(sdf_uint8_t *pucFileName, sdf_uint32_t uiNameLen) override {std::string file_name{reinterpret_cast<const char *>(pucFileName),uiNameLen};file_name = mgmt::get_md5_digest(file_name);remove(file_name.c_str());}

参考链接

  • istream::read
  • ostream::write
  • c++ fstream中seekg()和seekp()的用法 指定文件读取偏移值

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/446931.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android开发之Path详解

目录一、xxxTo方法1、lineTo(float x, float y)2、moveTo(float x, float y)3、arcTo3.1、arcTo(RectF oval, float startAngle, float sweepAngle)3.2、arcTo(RectF oval, float startAngle, float sweepAngle,boolean forceMoveTo)3.3、arcTo(float left, float top, float r…

git大文件拷贝代码命令

git clone 链接 --recursive

Android APK打包流程

目录一、概述二、打包流程1、打包资源文件&#xff0c;生成R.java文件2、处理aidl文件&#xff0c;生成相应的Java文件3、编译项目源代码&#xff0c;生成class文件4、转换所有的class文件&#xff0c;生成classes.dex文件5、打包生成APK文件6、对APK文件进行签名7、对签名后的…

使用openssl,实现输入和输出都是字符串的类型,注意:输入最好是16的倍数

头文件crypto_util.h #pragma once#include <string>namespace hsm{namespace mgmt{void get_md5_digest(const std::string &data,uint8_t result[16]);std::string aes_encrypt_to_string(const std::string &string,const std::string &password);std::s…

Android Studio 安装ASM插件

目录一、安装步骤1、Android Studio -> Preferences...2、Plugins -> Browse repositories...3、搜索ASM -> 选中要安装的插件 -> 右侧点击Install4、安装完后点击Restart Android Studio5、Android Studio重启后右侧会有个ASM图标6、找一个类右键 -> Show Byte…

使用openssl完成aes-cbc模式的数据加解密,输入和输出都是字符串的形式

代码 #include <cstring> #include <memory>#include <openssl/aes.h> #include <openssl/md5.h>namespace hsm{namespace mgmt{void get_md5_digest(const std::string &data,uint8_t result[16]){MD5_CTX md5_ctx{};MD5_Init(&md5_ctx);MD5…

Android 网络异常

目录前言一、UnknownHostException1、网络断开验证2、DNS 服务器意外挂掉验证3、DNS 服务器故障验证4、所需诊断信息二、ConnectTimeoutException三、SocketTimeoutException1、子错误 - 读超时2、子错误 - SSL 握手超时3、子错误 - 未知原因四、HttpHostConnectException1、服…

Android ViewRoot、DecorViewWindow浅析

目录简介目录1、VeiwRoot1.1、简介1.2、特别注意2、DecorView2.1、定义2.2、作用2.3、特别说明3、Window4、Activity5、之间关系5.1、总结5.2、之间的关系简介 DecorView为整个Window界面的最顶层View。DecorView只有一个子元素为LinearLayout。代表整个Window界面&#xff0c;…

Java集合Stream类

Java集合Stream类 ----按条件对集合进行过滤filter public class Test {public static void main(String[] args) {List<String>allnew ArrayList<>();all.add("ghjt");all.add("ghjiiii");Stream<String>streamall.stream();List<S…

使用openssl完成aes-ecb模式的数据加解密,输入和输出都是字符串类型

代码 #include <cstring> #include <memory>#include <openssl/aes.h> #include <openssl/md5.h>namespace hsm{namespace mgmt{void get_md5_digest(const std::string &data,uint8_t result[16]){MD5_CTX md5_ctx{};MD5_Init(&md5_ctx);MD5…

Java Stream MapReduce大数据开发模型

实现一个购买商品后,对数据进行处理统计的功能. 将购买的商品信息保存在Orders类中 public class Orders {private String name;private double price;private int amount;public Orders(String name, double price, int amount) {this.name name;this.price price;this.am…

随机函数的生成

函数代码 #include <string>bool GenerateRandom(std::string *str,unsigned int len) {srand(time(NULL));for(unsigned int i0;i<len;i){switch(rand()%3){case 1:(*str).push_back(Arand()%26);break;case 2:(*str).push_back(arand()%26);break;default:(*str).p…

Android 为控件设置阴影

在Android中设置一个阴影很简单&#xff0c;只需要两步&#xff1a; 设置eleavation值&#xff08;高度&#xff09;添加一个背景或者outline &#xff08;即阴影的形状&#xff09; 说明&#xff1a; View的大小位置都是通过x&#xff0c;y确定的&#xff0c;而现在有了z轴的…

Android在代码中设置drawableLeft(Right/Top/Bottom)

根据业务的需要&#xff0c;要在代码中设置控件的drawableLeft&#xff0c;drawableRight&#xff0c;drawableTop&#xff0c;drawableBottom属性。 我们知道在xml中设置的方法为&#xff1a; android:drawableLeft"drawable/xxxxx"但是在代码中并没有相关的setDr…

Java 冒泡排序

冒泡排序–时间复杂度n^2 对数组序列从前向后依次比较相邻两个元素的大小,若逆序则两个元素交换位置如果一趟下来没有发生交换,则说明序列有序,可以在序列中设置一个标志flag判断元素是否发生交换,从而来减少不必要的比较(在写完排序算法后再写)小结:一共进行数组大小-1次的外…

使用openssl开源AES算法,实现aes、aes-cbc和aes-ecb对字符串的加解密

注意事项 对于用户输入的密码进行了md5运算&#xff0c;从而保证数据格式的统一性 内部调用的随机函数&#xff0c;参考我的其他博文 参考链接 头文件crypto_util.h #pragma once#include <string>namespace hsm{namespace mgmt{void get_md5_digest(const std::strin…

Android学习指南

目录核心分析内容1、学什么1.1、Android基础 & 常用1.2、Android进阶1.3、与时俱进、热门技术1.4、编程语言&#xff1a;Java与Java虚拟机1.5、计算机基础1.6、总结2、怎么学2.1、学习路径&#xff1a;如何循序渐进、阶段性的学习Android的理论知识&#xff1f;2.2、获取途…

使用memcmp函数判断两个函数的前n位字节数是否相等

memcmp函数的介绍 头文件&#xff1a;#include <string.h>定义函数&#xff1a;int memcmp (const void *s1, const void *s2, size_t n);函数说明&#xff1a;memcmp()用来比较s1 和s2 所指的内存区间前n 个字符。字符串大小的比较是以ASCII 码表上的顺序来决定&#x…

java 选择排序

选择排序–时间复杂度n^2 第一次从arr[0]–arr[n-1]中选出最小值,与arr[0]交换 第二次从arr[1]–arr[n-1]中选出最小值,与arr[1]交换… 最小数:假定当前这个数是最小数,然后和后面的每个数进行比较,当发现有更小的数时,重定最小数与最小数的下标 总结: 选择排序一共有数组大…

Linux环境下实现unsigned char*向string的转换

代码 unsigned char input_data[input_data_length] {"This is my first encrypted plaintext hello world"}; openssl_enc_string hsm::mgmt::aes_ecb_encrypt_to_string(static_cast<string>((const char * )input_data),password);使用static_cast<st…