c++创建文件夹及其中的文件

代码展示

#include <iostream>
#include <direct.h>
#include <fstream>
#include <io.h>
using namespace std;
int main()
{
    if (_access(".\\testtmp", 0) == -1)/文件夹不存在时候
    {
        cout << "文件夹不存在的时候" << endl;
        if (_mkdir(".\\testtmp") == 0)
        {

            system("dir .\\testtmp");
            ofstream out_file;
            out_file.open("testtmp\\1.txt");
            out_file << "你在干嘛呢????" << endl;
            out_file.close();
            system("dir .\\testtmp");
        }
    }
    else文件夹存在的时候
    {
        cout << "文件夹存在的时候" << endl;
        system("dir .\\testtmp");
        ofstream out_file;
        out_file.open("testtmp\\2.txt");
        out_file << "你在干嘛呢????" << endl;
        out_file.close();
        system("dir .\\testtmp");
    }
    system("pause");
    return 0;
}
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++,可以使用文件流(fstream)来实现拷贝文件夹及其下面的所有文件。 以下是一个简单的例子: ```cpp #include <iostream> #include <fstream> #include <filesystem> void copyFolder(const std::string& source, const std::string& destination) { // 创建目标文件夹 std::filesystem::create_directory(destination); // 遍历源文件夹的所有文件和子文件夹 for (const auto& entry : std::filesystem::recursive_directory_iterator(source)) { // 构建目标文件文件夹路径 std::filesystem::path destinationPath = destination; destinationPath /= entry.path().string().substr(source.length()); if (entry.is_directory()) { // 如果是文件夹,创建目标文件夹 std::filesystem::create_directory(destinationPath); } else { // 如果是文件,拷贝文件 std::ifstream src(entry.path().string(), std::ios::binary); std::ofstream dst(destinationPath.string(), std::ios::binary); dst << src.rdbuf(); } } } int main() { // 拷贝源文件夹及其下面的所有文件到目标文件夹 copyFolder("/path/to/source/folder", "/path/to/destination/folder"); return 0; } ``` 这段代码使用了C++17文件系统库(filesystem)来实现拷贝文件夹及其下面的所有文件。首先,我们创建了目标文件夹,然后使用递归目录迭代器遍历源文件夹的所有文件和子文件夹。对于每个文件文件夹,我们构建目标文件文件夹路径,并检查它是文件还是文件夹。如果是文件夹,我们创建目标文件夹;如果是文件,我们使用文件流(fstream)来拷贝文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值