微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?