简体   繁体   English

C ++:重命名目录中的所有文件

[英]C++: Rename all files in a directory

List of whats been achieved and what I'm stuck on to help with understanding what I am asking 取得的成就以及我坚持要帮助理解的内容的列表

What I have achieved: 我所取得的成就:

Open a user specified directory, display all files within this directory. 打开用户指定的目录,显示该目录中的所有文件。

What I haven't yet achieved: 我尚未实现的目标:

Rename all files within this directory automatically according to a predefined name - Files are currently named as random characters, I wish to automatically rename them to "August 1", "August 2", "August 3" etc. Files have different extensions though, and I wish the extensions to remain the same. 根据预定义的名称自动重命名此目录中的所有文件-文件当前被命名为随机字符,我希望将其自动重命名为“ August 1”,“ August 2”,“ August 3”等。文件具有不同的扩展名,我希望扩展名保持不变。

So this is how I am opening and displaying the directory: 所以这就是我打开和显示目录的方式:

void DirectorySelector::OpenDirectory(void)
{
    // convert directory string to const char
    DIRECTORY = directory.c_str();

    pdir = opendir (DIRECTORY);
}

void DirectorySelector::DisplayDirectory(void)
{
    // read directory
    while (pent = readdir (pdir))
    {
        std::cout << pent->d_name << "\n";
    }
}

And this is what I am stuck on, renaming the files (files have different extensions, not sure if this will cause problems later on?) 这就是我所坚持的,重命名文件(文件具有不同的扩展名,不确定以后是否会引起问题吗?)

I get the following error as soon as the program hits the while loop: 一旦程序进入while循环,我将收到以下错误:

Unhandled exception at 0x009657C1 in MultipleRename.exe: 0xC0000005: Access violation reading location 0xCCCCCDE0. MultipleRename.exe中0x009657C1处未处理的异常:0xC0000005:访问冲突读取位置0xCCCCCDE0。

void DirectoryOperator::StandardRename(void)
{   
    i = 1;

    while (pent = readdir (pdir))
    {
        oldname = pent->d_name;
        newname = "August " + i;

        OLDNAME = oldname.c_str();
        NEWNAME = newname.c_str();

        rename(OLDNAME, NEWNAME);
        i++;
    }   
}

Note: All declarations handled elsewhere and have removed validation for simplicity, if you need the code I can post it. 注意:所有声明在其他地方都已处理,并且为了简化起见删除了验证,如果需要代码,我可以将其发布。 Also I have already checked that the directory is still open in the DirectoryOperator class and I am using MSVS2012 on Windows. 另外,我已经检查了DirectoryOperator类中的目录是否仍然打开,并且我正在Windows上使用MSVS2012。

Thanks in advance. 提前致谢。

There is a problem with the line: 这行有问题:

newname = "August " + i;

"August " is a char* and i is added to the pointer before it is converted into a std::string. “ August”是一个字符*,在将其转换为std :: string之前将其添加到指针中。

So, when i==1, your string will be "ugust ", and when it is 2, it will be "gust ". 因此,当i == 1时,您的字符串将为“ ugust”,而当其为2时,它将为“ gust”。 Very quickly, when i > 8, you will run into undefined behavior. 很快,当i> 8时,您将遇到未定义的行为。

Solutions: 解决方案:

newname = "August " + std::to_string(i); // c++11

or 要么

#include<sstream>  
...
stringstream ss;
ss << "August " << i;
newname = ss.str();

"I get the following error as soon as the program hits the while loop:" “程序一旦进入while循环,我就会收到以下错误:”

Unhandled exception at 0x009657C1 in MultipleRename.exe: 0xC0000005: Access violation reading location 0xCCCCCDE0.

Most probably pdir isn't correctly initialized when the code 代码很可能未正确初始化pdir

while (pent = readdir (pdir))

is called. 叫做。 The value 0xC0000005 indicates you're trying to dereference a nullptr somewhere. 0xC0000005表示您正在尝试在某处取消引用nullptr

Are you sure, that 你确定

pdir = opendir (DIRECTORY);

was called in sequence as intended, and the result was valid ( pdir != nullptr )? 被按预期顺序调用,并且结果有效( pdir != nullptr )?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM