Renaming a file to name of file that already exists

Solution 1:

Your code works and renames a file only once.

The second time you run your program, file banana.txt already exist in that directory and function std::rename("afile.dat" , "banana.txt"); returns an error code.

So you need to check if a file with the new filename is already exists or handle an error after function std::rename.