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

Hybrid View

  1. #1
    Join Date
    Apr 2008
    Posts
    163

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2008
    Posts
    163

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Apr 2008
    Posts
    163

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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 06:10 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Apr 2008
    Posts
    163

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured