首页 > 编程语言 >C++使用多线程将数据写入文件

C++使用多线程将数据写入文件

时间:2023-11-03 15:24:45浏览次数:33  
标签:std 文件 include 写入 C++ filename file 多线程

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

// 使用多线程将数据写入文件
void writeToFile(const std::vector<std::string>& data, const std::string& filename) {
    // 创建一个文件输出流
    std::ofstream file(filename, std::ios::out | std::ios::binary);
    // 如果文件无法打开,输出错误信息并返回
    if (!file) {
        std::cerr << "File could not be opened!" << std::endl;
        return;
    }

    // 创建一个线程向量
    std::vector<std::thread> threads;
    // 对数据中的每个字符串,创建一个新线程并将其写入文件
    for (const auto& str : data) {
        threads.push_back(std::thread([&]() {
            file.write(str.c_str(), str.size());
            }));
    }

    // 等待所有线程完成
    for (auto& t : threads) {
        if (t.joinable()) {
            t.join();
        }
    }

    // 关闭文件
    file.close();
}

// 主函数
int main()
{
    // 创建一个字符串向量
    std::vector<std::string> data = { "Hello", "World" };
    // 定义文件名
    std::string filename = "output.txt";
    // 调用函数,将数据写入文件
    writeToFile(data, filename);
}

 

标签:std,文件,include,写入,C++,filename,file,多线程
From: https://www.cnblogs.com/lizhiqiang0204/p/17807637.html

相关文章

  • C++中string类基本使用的详细归纳
    目录:string类的初始化操作实例化得到一个string类对象之后的常用成员函数的操作2.1从外部键盘获取输入的方式(注意与C风格字符串做区别)2.2比较string对象2.3遍历每个字符2.4string类中的insert()增加成员函数2.5string类中的erase()删除成员函数2.6常用基本操作......
  • Task异步多线程
    不废话,直接贴上代码...【1】直接实现多线程:`usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceTask异步多线程{classProgram{staticvoidMain(string[]args){......
  • C++使用new来初始化指向类的指针
    C++使用new来初始化类的指针1.ClassName*p=newClassName;调用默认构造函数。如果类里没有写默认构造函数,会使用编译器帮我们生成的,但不会初始化成员变量,如classNoConstructor//没写构造函数的类{public:~NoConstructor(){}voidprintVal(){......
  • c++实现哈希桶
    闭散列的回顾在前面的学习中我们知道了闭散列的运算规则,当两个数据计算得到的位置发生冲突时,它会自动的往后寻找没有发生冲突的位置,比如说当前数据的内容如下:当插入的数据为33时计算的位置为3,可是位置3已经被占领了并且4也被占领了,但是位置5没有被占领所以插入数据33就会占领位置5,......
  • c: Eclipse IDE for Embedded C and C++ Developers - 2023-09
    https://www.eclipse.org/downloads/            ......
  • c++ STL源码解读
    红黑树map,key不能添加相同的key,如果添加不会报错,但是添加不进去 #include<iostream>#include<map>#include<set>usingnamespacestd;intmain(intargc,charconst*argv[]){map<int,int>a;a.insert(make_pair(1,1));a.insert(make_pair(1,......
  • 文件名: ?Ciwindows\system32 inetsrconfiglapplicationHost.config 错误:无法写入配
    出现原因:出现这个问题,一般是在程序运行的时候更新程序,导致的.解决方案:MicrosoftWindows[版本6.3.9600](c)2013MicrosoftCorporation。保留所有权利。C:\Users\Administrator>netstopiisadmin/yIISAdminService服务正在停止....IISAdminService服务已成功停......
  • C++ 移动构造函数详解
    目录移动构造函数是什么?复制构造和移动构造对比改进的拷贝构造移动构造实现移动构造优点左值、右值、左值引用、右值引用std::move参考移动构造函数是什么?移动构造是C++11标准中提供的一种新的构造方法。先举个生活例子,你有一本书,你已经不想看了,但我非常想看,那么我有哪......
  • java网络编程与多线程
      一、Java 网络编程网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来。java.net包中J2SE的API包含有类和接口,它们提供低层次的通信细节。你可以直接使用这些类和接口,来专注于解决问题,而不用关注通信细节。java.net包中提供了两种常见的网络......
  • C++_点云和C++函数处理
    点云问题原始点云拼接-运动畸变是指在一帧时间内,激光雷达或者其载体在发生运动后,产生的点云位置不一样的问题点云是没有畸变的,每条激光线束最终会形成一个闭合的圆形===利用运动模型来做运动畸变补偿和ICP方式这些数据包进行点云组帧激光雷达重叠区域......