?
Solved

remove file

Posted on 2003-03-13
14
Medium Priority
?
250 Views
Last Modified: 2010-04-01
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
Comment
Question by:ami0714
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
  • +2
14 Comments
 
LVL 3

Expert Comment

by:elcapitan
ID: 8126234
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
 
LVL 3

Expert Comment

by:elcapitan
ID: 8126272
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 60 total points
ID: 8126403
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 8133967
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
 

Author Comment

by:ami0714
ID: 8141802
remove() or unlink() does not work here. But when this module stands alone in another program, it can remove the file. weird.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8142825
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
 
LVL 3

Expert Comment

by:elcapitan
ID: 8146201
Did you get the errno value?
--EC--
0
 

Author Comment

by:ami0714
ID: 8171415
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8171692
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
 
LVL 3

Expert Comment

by:elcapitan
ID: 8172682
>>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
 

Expert Comment

by:modulo
ID: 8182975
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
 
LVL 5

Expert Comment

by:Netminder
ID: 8185263
Instructions for how to finish the split left at http://www.experts-exchange.com/Community_Support/Q_20559349.html 

Netminder
EE Admin
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question