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;
}