'Why can't I delete and rename these files in C

I'm working on a small side project about file-handling in C, and here I have a list of rooms (which has been sorted) and I need to have a ViewList, borrowRoom, and returnRoom feature.

All the features are working well, except on the returnRoom feature. My method of retunrnRoom in deleting the specific line with the room number that the user inputted is by:

  1. Making the exact copy of the borrowed rooms list while skipping the specific line
  2. Deleting the old file and renaming the new one (since this is a small file, this method is compatible).

I managed to create the new file with the copied content and skipped line. However, the remove() function is not working (it is returning -1), thus rename() function can't be done as well. Here is my code snippet:

(I would like to note that the use of struct elements is from the struct room that I used in the other part of the code to store the status, borrower (PIC), and desc (Purpose))

void returnRoom(){
char enter[5];
system("cls");
printf ("+======+==========+=================+===============================================+\n");
printf ("| Room | Status   | PIC             | Purpose                                       |\n");
printf ("+======+==========+=================+===============================================+\n");
for (int j = 0; j < i; j++){
    if (strcmp (room[j].status, "Borrowed") == 0){
        printf ("| %d  | %s | %s", room[j].roomnum, room[j].status, room[j].borrower);
        for (int k = 0; k < 16 - strlen(room[j].borrower); k++){
            room[j].spaceborrow[k] = ' ';
            printf ("%c", room[j].spaceborrow[k]);
        }
        printf ("| %s", room[j].desc);
        for (int k = 0; k < 46 - strlen(room[j].desc); k++){
            room[j].spacedesc[k] = ' ';
            printf ("%c", room[j].spacedesc[k]);
        }
        printf ("|\n");
    }
}
printf ("+======+==========+=================+===============================================+\n");
printf ("-> Return Room\n");
returnroom:
int roomReturn;
int flag = 0;
printf ("Please input the room number [0 to cancel]: ");
scanf ("%d", &roomReturn);
getchar();
if (roomReturn == 0) return;
temp = fopen("usagenew.txt", "w");
for (int j = 0; j < i; j++){
    if (j == i-1 && roomReturn != room[j].roomnum){
        printf ("Room does not exist\n");
        flag = 1;
    }
    if (roomReturn == room[j].roomnum){
        if (strcmp(room[j].status, "Borrowed") == 0){
            for (int k = 0; k < i; k++){
                if (strcmp(room[k].status, "Borrowed") == 0){
                    if (roomReturn == room[k].roomnum) continue;
                    fprintf (temp, "%d#%s#%s\n", room[k].roomnum, room[k].borrower, room[k].desc);
                }
            }
            fclose (roomusage);
            fclose (temp);
            int deletefile = remove ("usage.txt");
            printf ("%d", deletefile);
            if (deletefile != 0) printf ("File failed to delete\n");
            else {
                int renamefile = rename ("usagenew.txt", "usage.txt");
                if (renamefile != 0) printf ("Renaming new file failed\n");
            }
            break;
        }
        if (strcmp(room[j].status, "Empty   ") == 0){
            printf ("Room is not borrowed by anyone\n");
            flag = 1;
            break;
        }
    }
}
if (flag == 1) goto returnroom;
scanf ("%[^\n]", enter);}

Weirdly enough, the code occasionally works correctly- it creates the new file as well as delete the old file. However, the new file won't pop out as it should be.

I guess there must be an error in my code, though I'm not sure where. Would really appreciate an answer pointing to where I am wrong and how to fix it, rather than just giving a complete and ready piece of code for me to use. Thank you! :)

UPDATE: I finally changed my method and decided to just overwrite everything in the same usage.txt file, thus (aside of some bugs that I am still trying to figure out) managed to get the returnRoom up and running. Thank you again :)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source