#Problem with remove() and rename()

64 messages · Page 1 of 1 (latest)

arctic needle
#

try running your program with sudo

opaque coral
waxen forum
#

Yea it’s windows 10

#

Forgot to mention

grave zephyr
#

its possible it just doesnt work on windows?

#

Or thats its open in another process/still open somehow

#

Check if the file is still there after it errors though

waxen forum
grave zephyr
#

Is the path correct?

waxen forum
#

yes, i'm able to open/edit the file with the same path

#

just can't delete it or rename the file

grave zephyr
#

honestly youre opening the same file multiple times in different branches and its possible you just miss one of your fcloses

#

why though

#

just open it once

#

and close it when youre completely done with it

waxen forum
#

i close it every time a branch finishes with it, tbh i thought that's how your meant to do it so it doesn't mess up anything

#

and so i open it again when another branch needs it

grave zephyr
#

also please fix your indentation

grave zephyr
#

It doesnt mean open it for each tiny time you use it and then immediately close it, when youre about to use it again anyway

waxen forum
#

alright i'll clean it up, if it doesn't work still i'll ask again

grave zephyr
#

also please read your warnings

#

I posted it into godbolt and it immediately gave me a bunch of warnings about your format specifiers

#

tbf theyre pretty silly warnings in this case but you should still not have them

#

whenever you have a character array e.g. char arr[...]; and you do &arr for printf/scanf it actually just wants arr because arrays decay to pointers

#

like char* ptr = arr is correct not char* ptr = &arr

#

I take immediate issue with this structure:

while (Admin == false) {
  while ((fscanf(fp, "%s %s", nameCheck, pwordCheck)) != EOF) {
    if (strcmp(username, nameCheck) == 0 &&
        strcmp(password, pwordCheck) == 0) {
       printf("\nLogin successful!\n");
       Admin = true;
       fclose(fp);
       break;
       }
   }

   if (Admin == true) {
     // Most of your code
   }
   else {
     //take user input again
   }
}
```what is the point of this
#

we loop while admin is false, okay makes sense, we want admin to be true

#

then we have a way to look if we could be admin or not

#

then when we're out of that instead of exiting the Admin == false loop we instead just do the work here instead

waxen forum
#

the while Admin == false is to keep the loop going if the user inputs the wrong username and passwords

#

so the user inputs wrong username/password, Admin continues to be false

#

it will keep asking user for username/password until Admin == true

grave zephyr
#

it does

#

but it does it really oddly

#

what it should look like is:

while(Admin == false) {
  while(...) {
  }

  if(Admin == true) break;

  //take user input again
}

//passed the admin check, we can now do the actual stuff we want to do
waxen forum
#

oooohhh

#

oh right

#

yea

grave zephyr
#

instead its horribly nested inside this loop

waxen forum
#

that looks way better lmao

grave zephyr
#

Basically its just making your code impossible to read

#

because everything is just happening over the top and inside of everything else

#

This also makes your file stuff much easier

#
admin_file = fopen("Admin file path");
while(Admin == false) {
  while(...) {
  }

  if(Admin == true) break;

  //take user input again
}

//Done checking the admin file forever
fclose(admin_file)
#

keeping things sequential is really helpful in c

#

whereas currently you have

if(Admin == true) {
  fclose(fp);
  //...
}

Now I see that and go ... well what if they get out of that branch without exiting the loop and Admin got set to false somewhere and now we need the file again

#

Its unlikely but it could happen if you mistype something whereas by making everything sequential and separated into their own blocks, file stuff becomes as simple as "open file once at start, close file once at end"

waxen forum
#

yea makes sense, I'm not planning out the structure of my code like I should be, causing me a lot of problems when trying to debug

#

thanks alot

grave zephyr
#

okay well really your files dont look too bad in the end

#

There is one branch route I could see maybe giving you an issue but I dont think it should

#

AH

#

found your issue

#

I was right

#

you have the file open twice

#
if (AdminOption == 1) {
    //...
    FILE* fpStaff;
    fpStaff = fopen(
        "C:\\Users\\Chong Yit Ren\\OneDrive\\Documents\\c "
        "programming text files\\StaffLogin.txt",
        "a+");
    //...
    if (NewStaff == 1) {
        // not important right now but there is a missed fclose in here
    }
    if (NewStaff == 2) {
        FILE* fpStaff;
        fpStaff = fopen(
            "C:\\Users\\Chong Yit Ren\\OneDrive\\Documents\\c "
            "programming text files\\StaffLogin.txt",
            "a+");
#

you create a second fpStaff

#

so it is still open when you close the inner one

#

this is why opening and closing files all over the place can be difficult. For the same reason that people don't like globals. They can be used all over the place and state interacts between branches and functions making it difficult to track

soft sluiceBOT
#

@waxen forum Has your question been resolved? If so, run !solved :)

waxen forum
#

@grave zephyr goddamn, thanks man, rly appreciate your help

#

!solved

soft sluiceBOT
#

Thank you and let us know if you have any more questions!