C++处理表标注信息,放到对应的图片文件夹中

C++编程:

1.采用windows api;

2.将.\label文件夹下的所有txt文件挨个存放到容器中;

3.读取一个.\video文件夹下的一级子目录路径,将容器中的一个文件放入,读取下一个.\video文件夹下的一级子目录路径,将容器中的下一个文件放入,依次类推;

#include <iostream>
#include <windows.h>
#include <vector>
#include <string>
#include <fstream>

std::vector<std::wstring> GetAllTxtFiles(const std::wstring& folderPath)
{
    std::vector<std::wstring> txtFiles;

    std::wstring searchPath = folderPath + L"\\*.txt";
    WIN32_FIND_DATAW fileData;
    HANDLE hFind = FindFirstFileW(searchPath.c_str(), &fileData);
    if (hFind != INVALID_HANDLE_VALUE)
    {
        do
        {
            if ((fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
            {
                std::wstring txtFileName = fileData.cFileName;
                txtFiles.push_back(txtFileName);
            }
        } while (FindNextFileW(hFind, &fileData));

        FindClose(hFind);
    }

    return txtFiles;
}

bool MoveFileToFolder(const std::wstring& filePath, const std::wstring& destinationFolderPath)
{
    std::wstring destinationPath = destinationFolderPath + L"\\" + filePath;

    if (!MoveFileW(filePath.c_str(), destinationPath.c_str()))
    {
        std::wcout << L"Failed to move file: " << filePath << std::endl;
        return false;
    }
    else
    {
        std::wcout << L"Moved file: " << filePath << L" to: " << destinationPath << std::endl;
        return true;
    }
}

std::wstring ReadFileContent(const std::wstring& filePath)
{
    std::wifstream file(filePath);
    if (!file)
    {
        std::wcerr << L"Failed to open file: " << filePath << std::endl;
        return L"";
    }

    std::wstring content;
    std::wstring line;
    while (std::getline(file, line))
    {
        content += line + L"\n";
    }

    file.close();

    return content;
}

int main()
{
    std::wstring labelFolder = L".\\label";      // label文件夹路径
    std::wstring videoFolder = L".\\video";      // video文件夹路径

    // 获取所有txt文件
    std::vector<std::wstring> txtFiles = GetAllTxtFiles(labelFolder);

    // 获取video文件夹下的一级子目录路径
    std::wstring searchPath = videoFolder + L"\\*";
    WIN32_FIND_DATAW fileData;
    HANDLE hFind = FindFirstFileW(searchPath.c_str(), &fileData);
    if (hFind != INVALID_HANDLE_VALUE)
    {
        size_t txtIndex = 0;

        do
        {
            if ((fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
            {
                std::wstring subFolderName = fileData.cFileName;

                if (subFolderName != L"." && subFolderName != L"..")
                {
                    // 读取txt文件并将其放入当前子目录
                    if (txtIndex < txtFiles.size())
                    {
                        std::wstring txtFile = txtFiles[txtIndex];
                        ++txtIndex;

                        std::wstring txtFilePath = labelFolder + L"\\" + txtFile;
                        std::wstring destinationFolderPath = videoFolder + L"\\" + subFolderName;

                        // 创建目标子目录
                        CreateDirectoryW(destinationFolderPath.c_str(), nullptr);

                        // 读取txt文件内容
                        std::wstring txtContent = ReadFileContent(txtFilePath);

                        // 将txt文件内容保存到目标子目录中
                        std::wstring destinationFilePath = destinationFolderPath + L"\\" + txtFile;
                        std::wofstream destinationFile(destinationFilePath);
                        if (destinationFile)
                        {
                            destinationFile << txtContent;
                            destinationFile.close();
                            std::wcout << L"Moved file with content: " << txtFilePath << L" to: " << destinationFilePath << std::endl;
                        }
                        else
                        {
                            std::wcerr << L"Failed to create file: " << destinationFilePath << std::endl;
                        }
                    }
                    else
                    {
                        std::wcout << L"No more txt files to move." << std::endl;
                        break;
                    }
                }
            }
        } while (FindNextFileW(hFind, &fileData));

        FindClose(hFind);
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值