首页 > 其他分享 >cpp 实现自定义更改文件名

cpp 实现自定义更改文件名

时间:2022-11-26 21:33:08浏览次数:27  
标签:std handle 自定义 文件名 file cpp txt png

任务要求:

指定文件夹中只有两种文件,png图片和txt文件。

如果有一对png图片和txt文件的文件名相同,则将png图片的文件名更改为txt文件的内容。

细节要求:

1、txt只有一行,由多个单词组成,每个单词之间由一个逗号和一个空格组成,如 "hello, apple, nihao",要求最终文件名不超过230字符情况下输出最多的单词,必须保证每个单词都完整,且最后一个单词后面没有逗号和空格。

2、如果file.png图片没有对应相同文件名的file.txt文件,应当如下的报错:

Don't have "file.txt" file.

如果file.txt没有对应的file.png文件,则无视。

输入:

文件路径,如C:\user (保证路径正确、路径最后面没有反斜杠)

 

思路:

  • 使用struct _finddata_t的name成员获取文件名 filename.png
  • 使用_findfisrt(path\*.png, _finddata_t*)、_findnext(handle, _finddata_t*)来遍历整个文件夹
  • 储存没有后缀的文件名filename在files list中
  • 使用fstream打开filename.txt文本,读入失败则报不存在该txt文本
  • 输入流直接读入txt内容(会直接删除掉空格),然后记录总长度,保持总长度小于230,超过230则跳出
  • 判断最后一位是否为逗号,为逗号则删除逗号
#include <iostream>
#include <fstream>
#include <vector>
#include <io.h>
#define cout std::cout
#define cin std::cin
#define endl std::endl

class RenamePngFile {
public:
    typedef std::vector< std::string > FileList;
    typedef std::string FilePath;
    typedef std::string FileName;

private:
    FilePath sThisPath;
    FilePath sAllPngPath; // path for png
    void GetFileName(const FilePath &sPath, FileList &sFiles); // get all png files
    void GetPath(); // get path for variable "sAllPngPath"
public:
    void ReAllName();
};

void RenamePngFile::GetPath() {
    cout << "Please enter the file path: " << endl;
    cin >> sThisPath;
    this->sAllPngPath = sThisPath;
    sAllPngPath.append("\\*.png"); // *.png files
}
void RenamePngFile::GetFileName(const FilePath &sPath, FileList &sFiles) {
    _finddata_t thisFile;
    intptr_t handle = _findfirst(sPath.c_str(), &thisFile); // handle of files
    if (handle != -1) { // not empty file
        do {
                FileName sThisFileName = thisFile.name;
                sThisFileName.erase(sThisFileName.end() - 4,sThisFileName.end());
                sFiles.push_back(sThisFileName);
        } while( _findnext(handle, &thisFile) == 0 );
    }
    _findclose(handle); // close;
}
void RenamePngFile::ReAllName() {
    FileList lPngFiles;
    std::ifstream ifsTxtFile; // for input txt files

    this->GetPath(); // get file path

    GetFileName(this->sAllPngPath, lPngFiles); // get all png files

    for(auto i = lPngFiles.begin(); i != lPngFiles.end(); i++) { // rename
        ifsTxtFile.open(this->sThisPath + "\\" + *i + ".txt"); // open same name txt
        if (!ifsTxtFile) { // if open failed to open
            cout << "Don't have \"" << *i <<".txt\" file." << endl;
            continue;
        }
        else {
            std::string sInputText;
            std::string sFinalText;
            unsigned long long length = 0;
            FilePath oldPath;
            FilePath newPath;

            while (!ifsTxtFile.eof()) {
                ifsTxtFile >> sInputText;
                length += sInputText.size();
                if (length <= 230) {
                    sFinalText.append(sInputText);
                } else {
                    break;
                }
            }
            if (sFinalText[sFinalText.size() - 1] == ',') {
                sFinalText.erase(sFinalText.end() - 1);
            }

            oldPath = this->sThisPath + "\\" + *i + ".png";
            newPath = this->sThisPath + "\\" + sFinalText + ".png";
            if(std::rename(oldPath.c_str(),newPath.c_str()) != 0) {  // rename
                cout << "\"" << *i << ".png\" rename failed." << endl;
            }
        }
        ifsTxtFile.close(); // close
    }
}

int main() {
    RenamePngFile doit;
    doit.ReAllName();
    return 0;
}

 

标签:std,handle,自定义,文件名,file,cpp,txt,png
From: https://www.cnblogs.com/zExNocs/p/16928348.html

相关文章