0

The background is the following: I search for an ID I want to replace and then I look at my file MedicalStore.txt for it. If I find it I replace it with another line of or record that didn't previously exist in the file. I make up another temporary file and copy-paste all the data with the exception of the searched ID which I replace using an If condition. I will attach the file as well.

            Modify(int SiD){
            struct customerinfo{
            char Prefix[20];
            char Name[20];
            int ID;
            unsigned long int Pnum;
            };
            struct customerinfo customer;
            FILE * Fptr;
            FILE * Ftemp;
    Fptr = fopen("MedicalStore.txt","r");
    Ftemp = fopen("replace.txt","w");
    char singleLine[150],newline[150],prefix[10],name[20];
    int id,c=0;
    unsigned long int num;
    while (!feof(Fptr)){
    fgets(singleLine,150,Fptr);
    c++;
    sscanf(singleLine,"%s %s %d %d\n",prefix,name,&id,&num);
    //printf("%s %s %d %d\n",prefix,name,id,num);
    if (id == SiD){
    strcpy(customer.Prefix,"Customer");
    printf("Enter Customer Name:\n");
    fflush(stdin);
    gets(customer.Name);
    printf("Enter unique ID of Customer : ");
    scanf("%d",&customer.ID);
    printf("Enter phone number of customer : ");
    scanf("%d",&customer.Pnum);
    printf("%d",customer.Pnum);
    sprintf_s(newline,150, "%s %s %d %d\n",customer.Prefix,customer.Name,customer.ID,customer.Pnum);
    fputs(newline,Ftemp);
    } else {
    fputs(singleLine,Ftemp);
    }
    }
    fclose(Fptr);
    fclose(Ftemp);
    remove("MedicalStore.txt");
    rename("replace.txt","MedicalStore.txt");
    return 0;
    }

Before editing with the code I replaced the 2nd line with another record

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 4
    Was the indentation lost in copy/paste, or your actual code looks like this? – Eugene Sh. Dec 16 '21 at 19:50
  • 1
    The description for the removed function `gets` starts with: _**Never use this function**_. Read about why and you will never use it again. – Ted Lyngmo Dec 16 '21 at 19:51
  • 1
    You may want to read this: [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/12149471) – Andreas Wenzel Dec 16 '21 at 19:57
  • @EugeneSh. I am new to the forum and I am kinda new to the programming world as well so please bear with my stupidity – Syed Muhammad Ismail Dec 16 '21 at 20:02
  • @SyedMuhammadIsmail This is not about stupidity, but if your actual code really looks like this you will make yourself a huge favor by learning how to indent it properly. – Eugene Sh. Dec 16 '21 at 20:04
  • I have tried to make the most out of my time by editing several source file for my project which is why some indentation would have lost/ignored by me. I am truly sorry for this – Syed Muhammad Ismail Dec 16 '21 at 20:08

1 Answers1

1

The problem is the condition in the while loop

while (!feof(Fptr)){
fgets(singleLine,150,Fptr);
//...

The condition can occur only after the following call of fgets. So if fgets encountered EOF the value of the string singleLine was not changed, It keeps the previous entered data. As a result the last line of the file is processed two times.

Instead you need to write

while ( fgets(singleLine,150,Fptr) != NULL ) {
//...

Pay attention to that this call

 fflush(stdin);

has undefined behavior.

Also the function gets is unsafe and is not supported by the C Standard.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I couldnt continue with the processing without fflush(stdin) because my gets functions was not working. Also got to know from you guys that gets function is unsafe. I will try to not use it from now on and will spread the word as well – Syed Muhammad Ismail Dec 16 '21 at 20:06