0
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <string.h> 


FILE *fp,*fp1;
char  p_id[7],buf[7],family[15],cl[15],nm[20];
int flag3=0;


int main()
{       // unsigned char t='y';

        clrscr();
        fp=fopen("Product.txt","r+");
        fp1=fopen("product_id.txt","w+");
        puts("\t\t\t Remove Product Details");
        printf("\n User :");
        if(flag3==0)
            {
                printf("\n\n\n \t\t\t Enter Product ID :");
                scanf("%s",p_id);
            }
        else
            {
                printf("\n\n\n \t\t\t Re-Enter Product ID :");
                scanf("%s",p_id);
            }
                 fclose(fp);
        fp=fopen("Product.txt","r");
        fscanf(fp,"%s %s %s %s",buf,family,cl,nm);
        while(!feof(fp))
        {
        if(strcmp(p_id,buf)==0)
            {
                printf("\n\n\n \t\t\t product removed");
            }
        else
            {
                fprintf(fp1," %s %s %s %s\n",buf,family,cl,nm);
            }
                fscanf(fp,"%s %s %s %s",buf,family,cl,nm);

        }
        //printf("\n \n Do you want to remove more product ?(Y/N)");
        //scanf("%s",&t);
        fclose(fp1);
        fclose(fp);
        remove("Product.txt");
         int status= rename("product_id.txt","Product.txt");
         if(status==0)
         {
             printf("working");
        }
         else 
         {
             printf("not working");
         }  
        getch();
        return 0;


}

In this code I am trying to update a specific line in a file by taking updated data store it into another file and then remove original file and rename 2nd file .Updation is working properly but rename() is not working.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • 1
    [while-feof-file-is-always-wrong](http://stackoverflow.com/a/5432517/3386109) – user3386109 Jun 16 '15 at 16:02
  • I believe it has something to do with file operations being not synchronous. It might happen that your `Product.txt` is still there when trying to overwrite it. What is the error code returned (my guess is `EBUSY`)? – Eugene Sh. Jun 16 '15 at 16:04
  • 3
    By "not working" do you mean that `rename()` returns a non-zero value, as judged by the program printing "not working"? In that case, it might illuminate the problem to examine and or print the actual return value, or better, to print a corresponding diagnostic by calling `perror()`. – John Bollinger Jun 16 '15 at 16:07
  • @user3386109 while_feof-file here is actually working fine and give correct output .problem I m facing here is only with remove() and rename(). – ameyCU Jun 16 '15 at 16:11
  • 1
    @ameyCU It can't work correctly. It *seems* to work correctly. Your last `fscanf` can't succeed. An then in the last iteration you are invoking an undefined behaviour comparing to `buf`. – Eugene Sh. Jun 16 '15 at 16:15
  • By perror() I am getting error File Exists. – ameyCU Jun 16 '15 at 16:18
  • 3
    @ameyCU That means `remove()` failed. Do perror on remove()'s failure. – P.P Jun 16 '15 at 16:20
  • @BlueMoon remove() gives error Permission Denied – ameyCU Jun 16 '15 at 16:23
  • That would do it, certainly. – WhozCraig Jun 16 '15 at 16:23
  • @user3386109 using `feof()` is often wrong, but not here, since OP uses it *after* trying to read from file and not (as in the usual howler) *before* reading the file. – Weather Vane Jun 16 '15 at 16:25
  • @WeatherVane Yes, my mistake as well..Update: Actually no. If for some reason the file have some additional characters not corresponding to the format, it will still be UB. – Eugene Sh. Jun 16 '15 at 16:26
  • @WeatherVane It is giving Permission Denied for remove() so what should I do now ? – ameyCU Jun 16 '15 at 16:28
  • @ameyCU Well, give the permission to the file then. Did you create the file as same user as you are running the program? – P.P Jun 16 '15 at 16:28
  • @EugeneSh. in which case the fault would be not checking the return value from `fscanf` – Weather Vane Jun 16 '15 at 16:32
  • @BlueMoon yes I created file as same user. – ameyCU Jun 16 '15 at 16:32
  • @WeatherVane Update to my Update: You are right. Looks like I am tired.. – Eugene Sh. Jun 16 '15 at 16:34
  • @WeatherVane fscanf returns 4. – ameyCU Jun 16 '15 at 16:36
  • That's good practice but not really the issue. – Weather Vane Jun 16 '15 at 16:36
  • @BlueMoon nothing is working same error as before . – ameyCU Jun 16 '15 at 16:44
  • I compiled and ran your program under MSVC and it worked (apart from badly formatted user messages). I don't suppose you still have `Product.txt` open in a text editor or elsewhere do you that prevents it being deleted? Or `product_id.txt` to prevent it being renamed? – Weather Vane Jun 16 '15 at 16:49
  • @WeatherVane The text of my comment comes from the title of the other post, and it is simple advice to newbies that they shouldn't write code like that. The first issue is that the `fscanf` has to be repeated, violating the [DRY principle](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself). The second issue is that `fscanf` can fail without reaching the EOF resulting in an infinite loop. (Granted the second issue occurs with `%d`, not `%s`.) But the point is that the OP should not be writing code with `while(foef())` in it, and I made a simple comment to that effect. – user3386109 Jun 16 '15 at 16:58
  • 1
    @user3386109 it was sensible advice, far better to use `while (4 != fscanf(...))` – Weather Vane Jun 16 '15 at 17:12

0 Answers0