- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Rename a file in C++ on Unix platform
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-10-2008 08:08 AM
тАО06-10-2008 08:08 AM
Rename a file in C++ on Unix platform
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...
- Tags:
- rename
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-10-2008 08:26 AM
тАО06-10-2008 08:26 AM
Re: Rename a file in C++ on Unix platform
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-10-2008 11:22 PM
тАО06-10-2008 11:22 PM
Re: Rename a file in C++ on Unix platform
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-10-2008 11:34 PM
тАО06-10-2008 11:34 PM
Re: Rename a file in C++ on Unix platform
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2008 12:50 AM
тАО06-11-2008 12:50 AM
Re: Rename a file in C++ on Unix platform
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);
- Tags:
- mv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2008 03:42 AM
тАО06-11-2008 03:42 AM
Re: Rename a file in C++ on Unix platform
/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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2008 03:56 AM
тАО06-11-2008 03:56 AM
Re: Rename a file in C++ on Unix platform
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2008 05:00 AM
тАО06-11-2008 05:00 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2008 05:07 AM
тАО06-11-2008 05:07 AM
Re: Rename a file in C++ on Unix platform
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2008 05:09 AM
тАО06-11-2008 05:09 AM
Re: Rename a file in C++ on Unix platform
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2008 08:19 AM
тАО06-11-2008 08:19 AM
Re: Rename a file in C++ on Unix platform
> in unix and windows.
I thought that I explained how to do it, as
have others. I even explained how to avoid
the problem by writing your temporary file to
the right place, instead of to "/var/tmp".
Do you need someone to write the code for
you, or what? What's not clear yet?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2008 08:21 AM
тАО06-11-2008 08:21 AM
Re: Rename a file in C++ on Unix platform
Thanks alot........
It is working fine now.I have used Preprocessor directives for the compatiblity problem .
#ifdef UNIX
system("mv /var/tmp/xyz.pdf ./Result/abc.pdf");
#else
if(rename(tmpfn[0], filename))
{
map_msg_err("Can't rename temp-PDF file '%s' to '%s'\n", tmpfn[0], filename);
mexit(EXIT_PDFRENAME);
}
#endif
Once again thanking u all for ur valubale suggestions...:-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2008 08:28 AM
тАО06-11-2008 08:28 AM
Re: Rename a file in C++ on Unix platform
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2008 08:31 AM
тАО06-11-2008 08:31 AM
Re: Rename a file in C++ on Unix platform
As a new Forum member, welcome! If you are happy with the suggestions you have received, please read:
http://forums11.itrc.hp.com/service/forums/helptips.do?#28
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2008 08:35 AM
тАО06-11-2008 08:35 AM
Re: Rename a file in C++ on Unix platform
Yeah. Sometimes it'd be nice to know more
about the people who post these questions, so
you'd know what not to buy.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-11-2008 08:38 AM
тАО06-11-2008 08:38 AM
Re: Rename a file in C++ on Unix platform
Steven, we have the same point of view.
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!
