-1

What I'm trying to do is to replace a specific line within a txt file using C but for some reason after saying what line i want to change it just ended the exe.

void UpdateCustomerPortfolio()
{
    const char path[500] = "C:\\Users\\jason\\Desktop\\College stuff\\Final assessment\\Fundimentals of programming\\Accounts\\";
    int Ic[12],line;
    char txt[5] =".txt",num[100];
    FILE * fPtr;
    FILE * fTemp;
    char buffer[BUFFER_SIZE];
    char newline[BUFFER_SIZE];
    int count;
    
    fflush(stdin);
    
    printf("Input IC number: ");
    gets(Ic);
    
    strcat (path, Ic);
    strcat (path, txt);
    
    
    system("cls");
    
    printf("Enter the Information that requires an update:");
    printf("\n[01]First Name");
    printf("\n[02]Last Name");
    printf("\n[04]Home Address");
    printf("\n[05]Contact Number");
    printf("\n[06]Email Address\n");
    scanf("%d ",line);

This is the part for some reason it keeps ending after the user enters the line they want to change. I did try using fflush(stdin); here again but it still didn't work.

    printf("Replace '%d' line with: ", line);
    fgets(newline, BUFFER_SIZE, stdin);


    /*  Open all required files */
    fPtr  = fopen(path, "r");
    fTemp = fopen("replace.tmp", "w"); 

    /* fopen() return NULL if unable to open file in given mode. */
    if (fPtr == NULL || fTemp == NULL)
    {
        /* Unable to open file hence exit */
        printf("\nUnable to open file.\n");
        printf("Please check whether file exists and you have read/write privilege.\n");
        exit(EXIT_SUCCESS);
    }


    /*
     * Read line from source file and write to destination 
     * file after replacing given line.
     */
    count = 0;
    while ((fgets(buffer, BUFFER_SIZE, fPtr)) != NULL)
    {
        count++;

        /* If current line is line to replace */
        if (count == line)
            fputs(newline, fTemp);
        else
            fputs(buffer, fTemp);
    }


    /* Close all files to release resource */
    fclose(fPtr);
    fclose(fTemp);

    /* Delete original source file */
    remove(path);

    /* Rename temporary file as original file */
    rename("replace.tmp", path);

    printf("\nSuccessfully replaced '%d' line with '%s'.", line, newline);
}
F. Müller
  • 3,969
  • 8
  • 38
  • 49
Jason
  • 9
  • 3
  • Don't mix the input methods, they don't behave the same with newlines. See [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). And stop using `gets()` which is obsolete and is no longer part of the standard C library. – Weather Vane Nov 13 '21 at 08:38
  • Also please see [fgets() doesn't work after scanf](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf). – Weather Vane Nov 13 '21 at 08:39
  • Also note that [`fflush(stdin)` is a bug](https://stackoverflow.com/questions/2979209/using-fflushstdin). – CherryDT Nov 13 '21 at 08:47
  • You asked almost identical question yesterday. It was closed because of the lack of information. Do not ask new question only edit the new one.. Do not abuse the saystem – 0___________ Nov 13 '21 at 08:57

1 Answers1

-1

Your scanf should read scanf("%d", &line);

Homer512
  • 9,144
  • 2
  • 8
  • 25