PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 29, 2022

[FIXED] Why is my program printing 2 same lines in the last when I tried to rewrite some lines in an existing file on my computer?

 October 29, 2022     c, eof, file, file-handling, if-statement     No comments   

Issue

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


Solution

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.



Answered By - Vlad from Moscow
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing