dev C++中的C++重命名函数总是返回“权限被拒绝”

问题描述

我得到了一个文件列表,我想通过使用包含每个文件各自名称的 txt 重命名这些文件。当我的程序运行并尝试重命名我的文件时,它会返回“权限被拒绝”。 我所做的是检查授予的权限,并尝试以管理员身份执行,但没有任何效果,所以我认为我的代码中肯定存在一个我没有看到的严重错误

这是我的代码

#include <iostream>
#include <dirent.h>
#include <string>
#include <fstream>
#include <sstream>
#include <memory.h>


using namespace std;

int main(int argc,char *argv[]){
    //get both paths
    string directory,path;
    cout<<"Jump to directory : ";
    cin>>directory;
    cout<<"Give path where to read out : ";
    cin>>path;
    
    //directories cannot be string
    const char* opendirectory = directory.c_str();
    const char* path_to_dictionary = path.c_str();
    
    //open path to txt which contains list of episodes
    ifstream file;
    file.open(path_to_dictionary,ios::in);
    if (!file) {
        cout << endl << "Error occured while opening " << path << "..."<<endl;
        system("pause");
    }
    
    //open path where files need to be renamed
    DIR *dirDirectory; 
    struct dirent *dirreadDirectory;
    
    /*The opendir() function opens a directory stream corresponding to
       the directory name,and returns a pointer to the directory
       stream. The stream is positioned at the first entry in the
       directory.*/
       
    if((dirDirectory = opendir(opendirectory)) != NULL) {
    /*The readdir() function returns a pointer to a dirent structure 
    representing the next directory entry in the directory stream pointed 
    to by dirp. It returns NULL on reaching the end of the directory 
    stream or if an error occurred. */ 
    
        while ((dirreadDirectory = readdir(dirDirectory)) != NULL) {
            //cout<<(dirreadDirectory->d_name)<<endl; works!
        
            /*d_name field contains the null terminated filename*/
            /*save old filename,must be const char*,rename() ecxpects const char**/
            const char* oldName = dirreadDirectory->d_name; 
        
            /*datatype of the filename*/
            string datatype = ".mkv"; 
            string getNewName = "";
            
            /*gets every line of the dictonary and saves it to nname which will be the the new name of the file*/
            getline(file,getNewName);
            string temp = getNewName + datatype;
            const char* newName = temp.c_str();
            /*Changes the name of the file or directory specified by oldname to newname.*/
            //cout<<newName<<endl; works!
            if (rename(oldName,newName) != 0){
                perror("Error renaming file,file not present");
            }else{
                cout<<"File successfully renamed"<<endl;
            }
       }
       closedir(dirDirectory);
       file.close();
    }
    else{
        /*Could not open directory*/
        perror("Could not open directory");
    } 

    system("pause");
    return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...