Rename function return -1
Hello,
I used rename function to rename a file.
The file names are in std::string type variable.
The function usage is like this
Quote:
int result = rename (old_file_name.c_str(),new_file_name.c_str());
i expected result = 0 but i get result = -1.
Does this return code is due to any file pointer holding to this file?
Thanks
Dave
Re: Rename function return -1
-1 indicates that an error occured. What is the value of errno? rename returns an error if the old file name does not exist (2) or the new file name already exists (17). Once you know the value of errno, look it up in errno.h
Re: Rename function return -1
Hello,
Quote:
Once you know the value of errno, look it up in errno.h
which function will help me to know the errno:?
Already i returned -1 to my Left Hand Side variable ?
Thanks
Dave
Re: Rename function return -1
No function. errno is just a global variable that holds the error number arising from c standard functions. There is also a function - perror(..) which prints the error in english. Have a look at the include files cerrno/errno.h. Also have a look at http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx and http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx
This is an example
Code:
#include <string>
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
string oldf = "temp.txt";
string newf = "data.txt";
int res = rename(oldf.c_str(), newf.c_str());
if (res < 0) {
cout << "Problem with rename. Error code: " << errno << endl;
perror(NULL);
} else {
cout << "rename successfull!" << endl;
}
return 0;
}
Re: Rename function return -1
Hello.
I output the error message to stdout using perror().
I get Permission denied message.
Some how some of the process hold my targeted file. I don't know which handler hold the file, but i don't want those file handles any more !
Is there any via i can destroy unknown handles (fopens) and free my file from the same un conditionally.?
Thanks
Dave
Re: Rename function return -1
To find what which program has an open handle to a file look at
http://technet.microsoft.com/en-us/s...rnals/bb896655
this program can also be used to force close a handle.
PS Are you sure it's not a pure file permission issue? Have a look at AccessChk and AccessEnum at
http://technet.microsoft.com/en-us/s...rnals/bb545027
Re: Rename function return -1
Hello,
I agree with your suggestion.
But i am facing a difficulty. My application is multithreaded and one of the thread acquire lock on the file handle and wait till the application exit. (Its like a Global Variable). ie:- The global object is alive till the application exit. I intended to use the file in write mode with in the global object scope. (I don't know how to consolidate the source here).
I would like to ask a question so that if i call a process (my file write application) separately from my parent process will it help the write back operation?
Thanks
Dave
Re: Rename function return -1
Why do you want to rename a file in one thread to which another thread has a file handle lock? Are you using windows? How does the thread that aquires the file lock open the file (CreateFile?). If using CreateFile, what sharing mode are you using?