Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

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');
}
0
ami0714
Asked:
ami0714
  • 4
  • 4
  • 4
  • +2
1 Solution
 
elcapitanCommented:
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--
0
 
elcapitanCommented:
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--
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Try using:

unlink ( "overdue.dat" ) ;

Though actually, remove translates into a call to unlink () only, but you can still try it. Since you have closed the file before deleting it, I don't think that there should be any other problem.

Mayank.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
ami0714Author Commented:
remove() or unlink() does not work here. But when this module stands alone in another program, it can remove the file. weird.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
elcapitanCommented:
Did you get the errno value?
--EC--
0
 
ami0714Author Commented:
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"
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
elcapitanCommented:
>>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--

0
 
moduloCommented:
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
0
 
NetminderCommented:
Instructions for how to finish the split left at http://www.experts-exchange.com/Community_Support/Q_20559349.html 

Netminder
EE Admin
0
 
ami0714Author Commented:
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now