Rename function return -1
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Rename function return -1

  1. #1
    Join Date
    Apr 2008
    Posts
    159

    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
    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

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    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
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Apr 2008
    Posts
    159

    Re: Rename function return -1

    Hello,

    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

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    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;
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2008
    Posts
    159

    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

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    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
    Last edited by 2kaud; July 19th, 2013 at 07:10 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Apr 2008
    Posts
    159

    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

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,854

    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?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center