Running remove() gets permission denied error...

This is a discussion on Running remove() gets permission denied error... within the C Programming forums, part of the General Programming Boards category; OS: Vista Compiler: Visual Express 2008 I'm programming a game using C code where I have to rewrite data in ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    28

    Running remove() gets permission denied error...

    OS: Vista
    Compiler: Visual Express 2008

    I'm programming a game using C code where I have to rewrite data in a text file. My problem is deleting a file using the remove() function.

    So what want I do is create a temp file, write the new data in it, then copy the other data from the other file into the temp file, and then finally delete the old file and rename the temp to the old file.

    I've made sure the files are closed when I call the functions remove() and rename() and I get the permission denied error with remove() and of course rename() won't work because the old file still exists.

    I'm running VE as administrator, I've disabled the UAC, I've made sure the read-only boxes are unchecked, I've placed all the files in my user directory also to run it, the remove() function returns -1 which perror() retrieves "permission denied". The data going into the temp file is fine (if it's relevant). All my files have full control permissions for me and Administrator.

    I've searched all over and can't find anything on the fix. I started to believe it was because Vista keeps placing the text files in read only but the file properties explicitely has no check mark under the read only box.

    Is there a setting in the compiler I'm overlooking? Or is something in Vista?

    [edit: line 6, word 3: added 'want']
    Last edited by edomingox; 01-11-2009 at 09:49 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Does something simple, like create a file, close it and delete it work?

    If the simple test works, then there's something less obvious going on outside of your description of the problem.

    If it doesn't, you've got something really simple to post to the forum.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    28
    the deleting part doesn't work.. the rest does. I was just explaining what I wanted it to do. I'll edit that to be more clear.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    What you want is clear enough.

    What isn't clear is why it doesn't work.
    Start with a really simple experiment, and learn something from the outcome.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    28
    I've done this before in XP. I've got Vista now and the same function just doesn't work. There's nothing more to it than just:

    Code:
    remove("MainFile.txt");
    rename("Temp.txt", "MainFile.txt");
    This just doesn't seem to work now. I think it may have something to do with Vista trying to prevent unauthorized accessess to files. But I turned off the UAC and that still didn't work. And everything was created under my user name which is also the administrator. I even re-created the main file while in VE and it still fails. If it was something simple, I wouldn't be posting on here. I've tried everything up to looking up similar problems on google, to changing folder ownerships. This seems to be a common problem:
    http://www.followsteph.com/2007/06/1...sta-read-only/

    I just don't know how to get by this. Unless there's an easy way to open a text file, edit, and then close it without having to delete the file.

    Through experimenting, files that are created in my program CAN be removed with the remove() function. I'll try another experiment. I'll try creating the file, edit it manually and rename it as my main file and delete the old one, and try the same functions again to see if that works.

    result from experiment: still fails.

    Ok, here's the weird thing. I just made this program and it worked.


    1. create a text file called test.txt and put whatever you want in it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        FILE *fpFileTo = {NULL};
        FILE *fpFileFrom = {NULL};
        char  readLine[80] = {0};
        
        if ((fpFileFrom = fopen("test.txt", "r")) == NULL) 
        {
            printfc(RED, "Could not find test.txt file.\n");
            system("PAUSE");
            return 0;
        }
        fpFileTo = fopen("temp.txt", "w");
    
        // write to the temp file
        fprintf(fpFileTo, "test1.\n");
        fprintf(fpFileTo, "test2.\n");
        fprintf(fpFileTo, "test3.\n");
    
        // add data from the test file
        while (fgets(readLine, 80, fpFileFrom))
              fprintf(fpFileTo, "%s", readLine);
    
        // close the files
        fclose(fpFileFrom);
        fclose(fpFileTo);    
    	
        // try the part that fails
        printf("remove: %d ", remove("test.txt"));
        perror("error:");
        printf("rename: %d ", rename("temp.txt", "test.txt"));
        perror("error:");
        system("PAUSE");
    }
    This tells me there must be somthing specific with my file. The weirder thing still is that I have already deleted the file and tried it from scratch and it didn't work. In the meantime, I guess if anyone needs to know how to edit a file, this program above is a good example.

    Confirmed: I deleted my original file. Created a new one, with a different name. Changed it properly in my program. Re-ran the program and it still fails with a permission denied.

    New Discovery: This works in main, but not in my subfunction in main. Will have to look more into that.

    Solution: Found out what the problem was. I had an earlier function that would open the file to read information. Turns out I never closed it. I was always under the belief that the file closes automatically when leave the function. I guess that only applies in main. From now on I will fclose() all my files just to be safe. Sorry for bothering everyone. Hopefully, some people can use my example.
    Last edited by edomingox; 01-11-2009 at 01:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21