美文网首页
c++ 读写文件

c++ 读写文件

作者: wasdzy111 | 来源:发表于2021-09-02 14:23 被阅读0次

引用原文址

#include <iostream>
#include <thread>
#include <fstream>

using namespace std;


int main() {
    // 1. 打开图片文件
    ifstream is("/home/mrlong/桌面/1.jpeg", ifstream::in | ios::binary);
    // 2. 计算图片长度
    is.seekg(0, is.end);
    int length = is.tellg();
    cout << length << endl;
    is.seekg(0, is.beg);
    // 3. 创建内存缓存区
    char *buffer = new char[length];
    // 4. 读取图片
    is.read(buffer, length);

    //二、另存为 223.jpg
    std::string strFile;
    for (size_t i = 0; i < length; i++) {
        strFile += buffer[i];
    }
    //1.打开保存文件,没有自动创建
    ofstream fout("/home/mrlong/桌面/21.jpeg", ios::binary);
    if (!fout)
        return 0;
    if (!fout) {
        cout << "文件不能打开" << endl;
    } else {
        //2.输出到磁盘文件
        cout << "文件大小:" << strFile.size() << endl;
        fout.write(strFile.c_str(), strFile.size());
        fout.close();
    }
    delete[] buffer;
    buffer = NULL;
    return 0;
}

相关文章

  • 2019-03-06 C++二进制文件结构体读取问题

    C与C++的二进制文件读写 参考下面的文章,C/C++读写文本文件、二进制文件 https://blog.csdn...

  • 文件读写总结

    1. C++文件读写详解 1.1. 文件读写操作 使用方式 1.1.1. 打开文件 文件操作通过成员函数open(...

  • c++ 积累

    c++读写文件 写文件 读文件 sudo ln -s /usr/local/cuda-9.1 /usr/local...

  • c++文件读写

    ifstream ifs;连续读写文件时: ifstream 是有状态的对象,一个 ifstream 操作完后一般...

  • C++文件读写

    1、 定义数据流对象指针 对文件进行读写操作首先必须要定义一个数据流对象指针,数据流对象指针有三种类型,它们分别是...

  • c++ 读写文件

    写文件文本 读文件文本 读写二进制文件

  • c++文件读写

    c++的文件读写,其实要导入一个新的头文件,差不多每实现一个新的功能就要导入一个新的头文件,从这个角度来看,还是现...

  • C++ 读写文件

    文件操作 文本文件 写文件 读文件 接下来我们看下用string来进行读文件 第四种是每个字符进行操作

  • C++文件读写

    欲对文件进行读写操作,首先得包含fstream[https://www.jianshu.com/writer]头文...

  • c++ 读写文件

    引用原文址[https://blog.csdn.net/mengsuifengc/article/details/...

网友评论

      本文标题:c++ 读写文件

      本文链接:https://www.haomeiwen.com/subject/mizqwltx.html