Link to home
Start Free TrialLog in
Avatar of ami0714
ami0714

asked on

remove file

These codes works completely fine except the remove and rename part. I don't know why it just won't remove the overdue.dat file. Any solution for this?

void delete_rec(void)
{
     FILE *fo, *ftemp;
     fo=fopen("overdue.dat","r");
     ftemp=fopen("temp.dat", "w");
     int cus_no, found=0, target=0;;
     char name[max], address[max], status[3], choice, choice2;

     do{
          system("cls");
          fflush(stdin);
          title();
          printf("Delete a record\n");
          printf("***************\n\n");
          printf("Enter Customer Number: ");
          scanf("%d", &target);
         
          while(fscanf(fo, "%d\n%[^\n]\n%[^\n]\n%[^\n]\n", &cus_no, name, address, status)!=EOF)
          {
               
               if(target==cus_no)
               {
                    found=1;
                    printf("\nCustomer Number:    %d\n", cus_no);
                    printf("Name:               %s\n", name);
                    printf("Address:            %s\n", address);
                    printf("Status:             %s\n\n", status);
                    do{
                         fflush(stdin);
                         printf("Do you want to delete this record? [Y/N] ");
                         choice=tolower(getchar());
                         if(choice=='y')
                         {
                              do{
                                   fflush(stdin);
                                   printf("\nCAUTION: This record is about to be deleted. Proceed anyway? [Y/N] ");
                                   choice2=tolower(getchar());
                                   if(choice2=='y')
                                   {
                                        printf("\nRecord has been deleted.\n");
                                       
                                   }
                                   else
                                        if(choice2=='n')
                                        {
                                             fprintf (ftemp, "%d\n%s\n%s\n%s\n", cus_no, name, address, status);
                                        }
                                        else
                                             if(choice2!='y'&&choice2!='n')
                                                  error_msg();
                              }while(choice2!='y'&&choice2!='n');
                         }
                         else
                              if(choice=='n')
                                   fprintf (ftemp, "%d\n%s\n%s\n%s\n", cus_no, name, address, status);
                              else
                                   if(choice!='y'&&choice!='n')
                                        error_msg();
                    }while(choice!='y'&&choice!='n');
               }
               else
                    fprintf (ftemp, "%d\n%s\n%s\n%s\n", cus_no, name, address, status);    
          }
          fclose(fo);
          fclose(ftemp);
          remove("overdue.dat");
          rename("temp.dat", "overdue.dat");

          if(found==0)
          {
               do{
               fflush(stdin);
               printf("\nThe customer record you requested canot be found.\nDo you want to try again? [Y/N] ");
               choice=tolower(getchar());
               switch(choice)
               {
                    case 'n':
                         break;

                    default:
                         error_msg();
                    }
               }while(choice!='y'&&choice!='n');
          }    
     }while(found==0&&choice=='y');
}
Avatar of elcapitan
elcapitan

From MSDN:

remove() fails if:
1. The path specifies a read-only file.
2. The filename or path was not found or that the path specifies a directory.

rename will fail if:
1. File or directory specified by newname already exists or could not be created (invalid path); or oldname is a directory and newname specifies a different path.
2. File or path specified by oldname not found.
3. Name contains invalid characters.

In order to find the reason this functions failed, check the return value and errno.

--EC--
You check the errno with perror() or strerror():

if( remove( "overdue.dat" ) == -1 )
      perror( "Could not delete 'overdue.dat'" );
else
      printf( "Deleted 'overdue.dat'\n" );

--EC--
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The rename () function is not working only because the remove () function is also not working - since the file still exists, so rename () is being unable to change the name of "temp.dat" to an existing file-name.

If you manage to delete the file (let's say by using unlink () instead of remove ()), the problem is solved.

Mayank.
Avatar of ami0714

ASKER

remove() or unlink() does not work here. But when this module stands alone in another program, it can remove the file. weird.
Hmmmm.... very strange. Is there a violation - I mean, is the file read-only or something?

What environment are you working upon? I'm sure if we're able to delete the first file, the rename will automatically work.

Mayank.
Did you get the errno value?
--EC--
Avatar of ami0714

ASKER

I solve the problem already. I have modify codes in the other module and now it can remove the file. both remove() and unlink() also works very fine.

Mayank, what's wrong if the file is read only? Is it mean if I open the file in "r" then it cannot be remove?

elcapitan, when I did errno, it display message "cannot delete "overdue.dat": permission denied"
If the file is read-only, then you won't be able to make any changes into it through your program. Also, you won't be able to delete it from your program. It might give some message like "Permission denied. ". You'll have to first change the properties of the file and uncheck (deselect) the read-only checkbox (assuming you're working on Windows).

If you open the file in "r", then it means that you're opening an existing file for reading in your program - meaning that your program will only be able to read from it and not write to it (no matter whether the file is read-only or not). And yes, after closing the file, it can definitely be removed (if its not read-only).

Mayank.
>>when I did errno, it display message "cannot delete "overdue.dat": permission denied"

Checking the errno was the only thing you needed to do in order to locate the problem. From MSDN about remove():

Return Value

... returns 0 if the file is successfully deleted. Otherwise, it returns –1 and sets errno either to EACCES to indicate that the path specifies a read-only file, or to ENOENT to indicate that the filename or path was not found or that the path specifies a directory. This function fails and returns -1 if the file is open.

--EC--

Dear ami0714

I've refunded 10 points to enable you to accept the comment for one expert and to post a
"Points for <expertname>" Q for the other expert in the same topic area.
There is however a minimum of 20 points for a question...

Please:
1) Post the link to the original Q in the "Points for <expertname>" and
2) Add in the original Q a comment with the link to the "Points for <expertname>", thus the email notif will warn the expert.

modulo

Community Support Moderator
Experts Exchange
Instructions for how to finish the split left at https://www.experts-exchange.com/questions/20559349/points-for-elcapitan.html 

Netminder
EE Admin