rename file makes a copy of the file

Hello all,

I am trying to rename an existing file, but it gives me -1 as result and it makes a copy of the old file, named with the new name.

Is this perhaps because the existing file is somehow still in use? (It should not be, I've tried to open and close it right before the renaming)

Thanks!
--Cristina.
Alas, yes -- seems like my idea was correct. Using perror() I could see "Permission denied" as error message.

Is there any way to circumvent this? I know for a fact nobody uses the file because it is generated and closed in my app way before the renaming takes place.
Hi Cristina,

I followed the example on this page:

http://www.cplusplus.com/reference/clibrary/cstdio/rename/

and it worked fine for me. I'm on Windows and I just used a simple text file to test it. Is your code similar?

Do you have the right access level for this file?

Hope that helps.
Can you try it with a basic text file (that you create and have permissions for) and see if that works?
http://linux.die.net/man/2/rename
http://en.wikipedia.org/wiki/Rename_%28C%29

Calling a C function to do it does not trump user permissions or other OS-specific conditions. (For example, if you are on Windows the rename() function may fail if the target file already exists, instead of overwriting it as it is supposed to... a known bug.)

The best way to handle this kind of stuff is to check that everything is where they are supposed to be (or not be) before beginning, check permissions, rename, then check again to see if things are what they should have become.

Alas. Hope this helps.
Thank you both Sammy34 and Duoas, it seems the problem is related to my application! (Thread opening/closing file deals incorrectly with the closing part).

On a test file, the function is working properly.
Topic archived. No new replies allowed.