C++处理YTB faces数据集标注文件

该程序使用C++和WindowsAPI,处理.label目录下的所有TXT文件。它删除每行中第二个反斜杠前的字符,移除每行的第一个逗号及其后的一个字符,以及删除每行末尾的两个字符和两个逗号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++编写程序:
1.采用windows api;
2.对.\label文件夹下的txt做以下处理:
        删除每行文本中第二个\前的字符;
        删除每个txt文件每行的第一个逗号","及后面的第一个字符;
        删除每行的最后两个字符和两个;

原来数据行:Aaron_Eckhart\0\0.555.jpg,0,237,137,84,84,0.0,1

现在数据行:0.555.jpg,237,137,84,84

//C++编写程序:
//C++编写程序:
//1.采用windows api;
//2.对.\label文件夹下的txt做以下处理,
//删除每行文本中第二个\前的字符
//删除每个txt文件的每行的第一个逗号","及后面的第一个字符
//删除每行的最后两个字符和两个,

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

void ProcessTextFile(const std::wstring& filePath)
{
    std::wifstream inputFile(filePath);
    std::wstring tempFilePath = filePath + L".temp";
    std::wofstream tempFileStream(tempFilePath);

    if (!inputFile)
    {
        std::wcerr << L"Failed to open input file: " << filePath << std::endl;
        return;
    }

    if (!tempFileStream)
    {
        std::wcerr << L"Failed to open temporary file: " << tempFilePath << std::endl;
        inputFile.close();
        return;
    }

    std::wstring line;
    while (std::getline(inputFile, line))
    {
        if (!line.empty())
        {
            // Remove the first comma and the next character
            std::size_t commaPos = line.find(L',');
            if (commaPos != std::wstring::npos)
            {
                line.erase(commaPos, 2);
            }

            // Remove the last two characters and two commas
            if (line.size() >= 2)
            {
                line = line.substr(0, line.size() - 6);
            }

            // Remove characters before the last backslash
            std::size_t backslashPos = line.find_last_of(L'\\');
            if (backslashPos != std::wstring::npos)
            {
                line = line.substr(backslashPos + 1);
            }
        }

        tempFileStream << line << std::endl;
    }

    inputFile.close();
    tempFileStream.close();

    // Replace the original file with the temporary file
    if (MoveFileExW(tempFilePath.c_str(), filePath.c_str(), MOVEFILE_REPLACE_EXISTING) == 0)
    {
        std::wcerr << L"Failed to replace file: " << filePath << std::endl;
    }
    else
    {
        std::wcout << L"Processed file: " << filePath << std::endl;
    }
}

void ProcessTextFilesInDirectory(const std::wstring& directory)
{
    WIN32_FIND_DATAW fileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;

    std::wstring searchPath = directory + L"\\*.txt";
    hFind = FindFirstFileW(searchPath.c_str(), &fileData);

    if (hFind != INVALID_HANDLE_VALUE)
    {
        do
        {
            std::wstring filePath = directory + L"\\" + fileData.cFileName;
            ProcessTextFile(filePath);
        } while (FindNextFileW(hFind, &fileData) != 0);

        FindClose(hFind);
    }
}

int main()
{
    std::wstring directory = L".\\label";

    // Step 2: Process the txt files in the label directory
    ProcessTextFilesInDirectory(directory);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值