jeudi 10 juin 2021

C++ rename function in dev C++ always return "Permission denied"

I got a list of files which i want to rename by using a txt which contains the respective name for each file. When my program runs and tries to rename my files it returns me a "permission denied". What i did is checking the granted rights and i tried executing as admin, but nothing worked, so i think there muste be a critical mistake in my code which i dont see.

Heres my code:

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

Aucun commentaire:

Enregistrer un commentaire