c++ - cpp: error when using rename function -


i have following code:

void commandrenhandler::handlecommand(socket* socket, std::vector<std::string>& rvparams){     if (rvparams.size() < 2){         socket->writeline("not enough parameters specified");         return;     }      std::string filepath = base_directory;     filepath.append(rvparams.at(0));     std::string filepath2 = base_directory;     filepath2.append(rvparams.at(1));     int result = rename(filepath.c_str(), filepath2.c_str());      // test     std::cout << filepath << " " << filepath2 << std::endl << "result: " << result << std::endl;      if (result == 0)         socket->writeline("file renamed");     else         socket->writeline("could not rename file"); } 

in function receive 2 file-paths files need renamed. i've added test cout.

i've got 1 file need renamed. here input , output of tests:

1 (renaming file manually - works intented):

in console:

ren one\image.png one\new_image.png 

debugging:

filepath = "c:\\...\\one\\image.png" filepath2 = "c:\\...\\one\\new_image.png" result = 0; 

test output:

c:\...\one\image.png c:\...\one\new_image.png result: 0 

2 (renaming file manually - works intented):

in console:

ren one\new_image.png one\image.png 

debugging:

filepath = "c:\\...\\one\\new_image.png" filepath2 = "c:\\...\\one\\image.png" result = 0; 

test output:

c:\...\one\new_image.png c:\...\one\image.png result: 0 

3 (using sync uses same rename - not work):

in console:

sync 1 one 

(this method checks if file changed or renamed. in case it's renamed , send info below rename function, regular ren above.)

debugging:

filepath = "c:\\...\\one\\image.png" filepath2 = "c:\\...\\one\\new_image.png" result = -1; 

test output:

c:\...\one\image.png c:\...\one\image.png result: -1 

1 (rename same file again, in step 1, time error again - not work anymore)

in console:

ren one\image.png one\new_image.png 

debugging:

filepath = "c:\\...\\one\\image.png" filepath2 = "c:\\...\\one\\new_image.png" result = -1; 

test output:

c:\...\one\image.png c:\...\one\new_image.png result: -1 

so, question is: why rename function of c++ works fine @ first, fails during sync function , after i'm not able use ren function anymore manually..

tl;dr: can see problem parts of code provided? or there way see caused rename error? because -1 kinda useless debug-result..

thanks in advance.

ps: here sync code, in case:

std::vector<std::string> vparams; // check wether file different or renamed std::string oldfile = filejustrenamed(file); if (oldfile != "") {     vparams.push_back(std::string("ren"));     vparams.push_back(oldfile);     vparams.push_back(file);      std::cout << "renaming " << file << std::endl;     ren_handler->handlecommand(socket, vparams); } 

pss: yes, paths match exactly. "..." in filepaths privacy reasons.

and wondering: c++ coded in windows, not linux.

edit: first rvparam ("ren") removed in factory-class, creates commandrenhandler. there handlecommand function called first rvparam (in case "ren") removed. rvparams.at(0) first filepath , rvparams.at(1) second filepath.

sorry didn't mentioned before in code.

well, have off-by-one error:

if (oldfile != "") {     vparams.push_back(std::string("ren"));     vparams.push_back(oldfile);     vparams.push_back(file);      std::cout << "renaming " << file << std::endl;     ren_handler->handlecommand(socket, vparams); } 

here, vparams.at(0) "ren" string, you're using in rename:

std::string filepath = base_directory; filepath.append(rvparams.at(0));           // "ren" , not actual filename. std::string filepath2 = base_directory; filepath2.append(rvparams.at(1)); int result = rename(filepath.c_str(), filepath2.c_str()); 

so, based on see, guess correct implementation of handlecommand:

void commandrenhandler::handlecommand(socket* socket, std::vector<std::string>& rvparams){     if (rvparams.size() < 3){         socket->writeline("not enough parameters specified");         return;     }       if(rvparams.at(0) == "ren")     {         std::string filepath = base_directory;         filepath.append(rvparams.at(1));         std::string filepath2 = base_directory;         filepath2.append(rvparams.at(2));         int result = rename(filepath.c_str(), filepath2.c_str());          // test         std::cout << filepath << " " << filepath2 << std::endl << "result: " << result << std::endl;          if (result == 0)             socket->writeline("file renamed");        else             socket->writeline("could not rename file");     } } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -