#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0

    Problem with removing and renaming files.


    Hi devshed!

    So, I'm working on a project where I'm making a database for employee information of a hypothetical company.
    I've got most of it working, but I'm trying to implement a feature that compacts the record file. What I have it doing is it writes all the records not marked with a '*' to a separate file, then deletes the original database file and renames the other file to the name of the deleted one.

    Basically the code where this removing and renaming happens is like this:

    Code:
    if( remove( "database.dat" ) == 0 )
    {
       if( rename( "temp.dat", "database.dat" ) == 0 )
       {
           printf("Record compacted.");
       }
    }
    For some reason if the record file was created in the same instance of the program where the user tries to compact it, something goes wrong in this part of the code.
    The same problem occurs if an employee's information was written to the record in the same instance as a compact command is issued.

    Any help on why this is isn't working would be great, thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Are you sure you closed the file before you try to delete it?

    Also what part is failing, the deleting or the renaming?

    Both of these functions return values that indicate success or failure and they may also set the errno variable which you can use to help determine the actual cause of the failure.


    Jim
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0
    Originally Posted by jimblumberg
    Are you sure you closed the file before you try to delete it?

    Also what part is failing, the deleting or the renaming?

    Both of these functions return values that indicate success or failure and they may also set the errno variable which you can use to help determine the actual cause of the failure.


    Jim
    Thanks for replying, and that's what I checked for first; but it's definitely been closed properly.
    I think it's the deleting, because both the temp.dat and database.dat are present in the folder after it runs.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    If you're not sure you need to check the return values of the two functions. If they return a non-zero value check the value of the errno variable using perror(). Here is an example of using rename() that is checking and printing the errno value using perror() on failure. rename().


    Jim
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0
    Originally Posted by jimblumberg
    If you're not sure you need to check the return values of the two functions. If they return a non-zero value check the value of the errno variable using perror(). Here is an example of using rename() that is checking and printing the errno value using perror() on failure. rename().


    Jim
    I'll run it through the program now and get back to you, thanks.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0
    Originally Posted by jimblumberg
    If you're not sure you need to check the return values of the two functions. If they return a non-zero value check the value of the errno variable using perror(). Here is an example of using rename() that is checking and printing the errno value using perror() on failure. rename().


    Jim
    Apparantly it's the deleting that's causing it to fail.
    I got "Error deleting file: Permission denied"
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Originally Posted by Kamikageyami
    Apparantly it's the deleting that's causing it to fail.
    I got "Error deleting file: Permission denied"
    This message means that either the file is still being used, thus can't be deleted, or you don't have permission to delete this file. And since you say you can delete the file after you restart your program I suspect the file hasn't been properly closed.

    Jim
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    19
    Rep Power
    0
    Originally Posted by jimblumberg
    This message means that either the file is still being used, thus can't be deleted, or you don't have permission to delete this file. And since you say you can delete the file after you restart your program I suspect the file hasn't been properly closed.

    Jim
    /facedesk

    Thanks Jim, found the problem. In a different function I opened two file pointers, one pointing to that one. Accidentally closed the first twice. It's working fine now
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Don't forget to always check the return values, if provided. These return values can help you figure out where the actual problem is located, and insure the program runs without "hidden" problems.

    Jim

IMN logo majestic logo threadwatch logo seochat tools logo