Operating System - Linux
1753797 Members
7901 Online
108805 Solutions
New Discussion юеВ

Rename a file in C++ on Unix platform

 
Soumya Krishnan
New Member

Rename a file in C++ on Unix platform

Hi All,

I have a c++ code to rename a pdf file using io.h.While trying to run the same code in unix platform it is giving error message.Is there any way to rename in c++ which is compatible to unix.Or How could i rectify this problem.Please give a suggestion
c++ code
rename(tmpfn[0], filename)

Thanks in Advance
Soumya...
15 REPLIES 15
James R. Ferguson
Acclaimed Contributor

Re: Rename a file in C++ on Unix platform

Hi:

Well, 'rename()' is the Unix system call to change the name of a file (of course).

You need to specify what "error (message)" you are getting. You *should* be examining any 'errno' returned when the return status of the 'rename' isn't zero. That should tell you the reason for your failure.

I would also re-read the manapges:

http://docs.hp.com/en/B3921-60631/rename.2.html

For instance, are your source and target names in the same filesystem. If not, you can't 'rename()'.

Regards!

...JRF...
Soumya Krishnan
New Member

Re: Rename a file in C++ on Unix platform

Hii James,

Thanks a lot for your suggestion .
The error number i got is errno=18
[EXDEV]
The paths named by source and target are on different logical devices (file systems).
What shall i do for this.
I am trying to rename a file from the folder
/var/tmp/xyz.pdf to ./Result/abc.pdf
means from a system temporary folder to application folder
Steven Schweda
Honored Contributor

Re: Rename a file in C++ on Unix platform

> What shall i do for this.

What would you like to do? Many things are
possible. For example:

1) When you get EXDEV, quit.

2) When you get EXDEV, copy the file, and
then, if that works, delete the original
file.

3) Don't create the temporary file in
/var/tmp. Instead, create it in the
destination directory (with some funny name),
and when it's ready, rename it to the
desired name.

Some programs (Info-ZIP Zip, for example)
will try to do 3, but if the rename fails
for some reason, it'll try to do the job with
a copy. (And if that fails, it'll complain.)
Dennis Handly
Acclaimed Contributor

Re: Rename a file in C++ on Unix platform

>I am trying to rename a file from /var/tmp/xyz.pdf to ./Result/abc.pdf

As Steven says, you can't do that directly if not on the same filesystem.

If you are real lazy, you can use system(3) to invoke mv(1):
system("mv /var/tmp/xyz.pdf ./Result/abc.pdf");

Of course if the two files are variables, you would have to use strcpy and strcat to form the string. Or use:
snprintf(buf, sizeof(buf), "mv %s %s", from_file, to_file);
Torsten.
Acclaimed Contributor

Re: Rename a file in C++ on Unix platform

You want to "rename"

/var/tmp/xyz.pdf to ./Result/abc.pdf

You need to copy the source to the target and delete the source - well known as move.
If you give the target file another name while creating, you have what you want.

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
James R. Ferguson
Acclaimed Contributor

Re: Rename a file in C++ on Unix platform

Hi:

Now you see the immediate value of interrogating the 'errno' on failure of a system call, as I first suggested. Interestingly, too, your failure (EXDEV) was the one I suspected as I noted :-)

Using the shell 'mv' command does a *copy* in lieu of a 'rename' whenever a file resides in different filesystems. You could take this middle-ground in your code:

If the source and destination names aren't at the same root directory, you could assume that different filesystem may be involved and call 'system()' with 'mv', otherwise you could perform the 'rename()' directly.

Regards!

...JRF...
Soumya Krishnan
New Member

Re: Rename a file in C++ on Unix platform


Thanks all
system move command is working fine in unix..Its is not working in windows.But i need a way that renames the file both in unix and windows.
James R. Ferguson
Acclaimed Contributor

Re: Rename a file in C++ on Unix platform

Hi (again):

> But i need a way that renames the file both in unix and windows

use Perl;

Write a simple Perl script using the File::Copy module. This implements things in a platform independent fashion.

Regards!

...JRF...
Torsten.
Acclaimed Contributor

Re: Rename a file in C++ on Unix platform

If this is C code, you can do your own move.
Open the file, read it and write the contents to a new file. Close both and delete the source. Don't forget some error checking/exception handling.

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!