STD :: FSTREAM / STD ::文件系统创建使用重复号码的文件

发布于 2025-01-17 12:32:01 字数 209 浏览 1 评论 0原文

我想在文件末尾创建一个带有数字的文件: fileName(编号转到此处).txt。该号码会告诉我目录中是否有文件的重复项,创建了文件:filename.txt

示例:helloworld(1).txt

Windows尝试创建文件重复时也具有此功能。我可以在C ++ 17中这样做吗?

I want to create a file with a number at the end of the file :
filename ( number goes here ).txt. The number will tell me if there were duplicates of the file in the directory the file was created : filename.txt .

Example : helloworld (1).txt

Windows also has this functionality when trying to create a file duplicate. Is there way I could do this in C++ 17 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2025-01-24 12:32:01

由于没有人给我任何答案,我决定花一些时间在这个函数上,它可以做我想要的事情,并回答我自己的问题,它在 C++ 17 中,对于任何认为这有用的人:

#include <filesystem>
#include <regex>
#include <fstream>
#include <string>

namespace fs = std::filesystem;

// Just file "std::ostream::open()" except it handles duplicates too
    void create_file_dup(std::string path)
    {
        // Check if file doesnt have duplicates
        if (!fs::exists(path)) {
            std::ofstream(path);
            return;
        }

        // Get filename without path
        std::string filename = (fs::path(path).filename()).string();

        // Since its already a duplicate add "(1)" inbetween the basename and extension
        filename = (fs::path(filename).stem()).string() + " (1)" + (fs::path(filename).extension()).string();

        // Get file's parent directory
        std::string parent = (fs::path(path).parent_path()).string();


        // Loops to check for more duplicates
        for (int dup_count = 2;!fs::exists(parent + filename);dup_count++) {

            std::string dup_c = "(" + std::to_string(dup_count);      // old number
            dup_c + ")";

            std::string dup_cn = "(" + std::to_string(dup_count + 1);   // new number 
            dup_cn + ")";

            filename = std::regex_replace(filename, std::regex(dup_c), dup_cn); // increments : '(1)' -> '(2)'
        }

        // We have found the how many duplicates there are , so create the file with the duplicate number
        std::ofstream(parent + filename);
        return;
    }

Since no one is giving me any answers, I decided to spend some time on this function that does what I want , and answer my own question , its in C++ 17 for anyone who thinks this is useful :

#include <filesystem>
#include <regex>
#include <fstream>
#include <string>

namespace fs = std::filesystem;

// Just file "std::ostream::open()" except it handles duplicates too
    void create_file_dup(std::string path)
    {
        // Check if file doesnt have duplicates
        if (!fs::exists(path)) {
            std::ofstream(path);
            return;
        }

        // Get filename without path
        std::string filename = (fs::path(path).filename()).string();

        // Since its already a duplicate add "(1)" inbetween the basename and extension
        filename = (fs::path(filename).stem()).string() + " (1)" + (fs::path(filename).extension()).string();

        // Get file's parent directory
        std::string parent = (fs::path(path).parent_path()).string();


        // Loops to check for more duplicates
        for (int dup_count = 2;!fs::exists(parent + filename);dup_count++) {

            std::string dup_c = "(" + std::to_string(dup_count);      // old number
            dup_c + ")";

            std::string dup_cn = "(" + std::to_string(dup_count + 1);   // new number 
            dup_cn + ")";

            filename = std::regex_replace(filename, std::regex(dup_c), dup_cn); // increments : '(1)' -> '(2)'
        }

        // We have found the how many duplicates there are , so create the file with the duplicate number
        std::ofstream(parent + filename);
        return;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文