0

This is the code.

    FILE* fPtr;
    FILE* fTemp;
    char path[100];

    char buffer[BUFFER_SIZE];
    char newline[BUFFER_SIZE];
    int line, count;


    printf("Enter path of source file: ");
    scanf("%s", path);

    printf("Enter line number to replace: ");
    scanf("%d", &line);

    /* Remove extra new line character from stdin */
    fflush(stdin);

    printf("Replace '%d' line with: ", line);
    scanf("%s", &newline);


    /*  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)
    {
        /* 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)) != 0)
    {
        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);

    return 0;

I wanted to replace a line supposedly the content of the text file is this

> Andy,06/05/2000,US,0654852,254845,313132
> Fan,865644,4654654,654654,465456
> Ben,04/01/1995,SG,0674874,213454,132158

Supposedly I wanted to change the of Fan so I run the code above, it gave me this. I do not want this to happen.

> Andy,06/05/2000,US,0654852,254845,313132
> Fanny,865644,4654654,654654,465456Ben,04/01/1995,SG,0674874,213454,132158

And if I want to change the name of Andy it gave me this Landy,06/05/2000,US,0654852,254845,313132Fanny,865644,4654654,654654,465456Ben,04/01/1995,SG,0674874,213454,13215

Why it does that?

Lorale
  • 73
  • 6

4 Answers4

2

How do I delete specific line and replace it ?

Assume that the replacement line has a different size than the original one. You cannot do that in standard C11 (check n1570) without copying the file to a new place (because you cannot overwrite a sequence of bytes in the middle of a file by another sequence of different length).

Read carefully the documentation of <stdio.h>

Lines are just a convention in C: they are ending by some end-of-line character (\n). A file could have a single line and contain a megabyte.

So you could use getline to read lines. Or use fgets. In both cases you should check for failure. With fgets what would happen if the line is bigger than the buffer? With getline what would happen with a file containing a single line of a gigabyte which does not fit into memory?

Be aware that stdout is buffered (and the buffer size could vary from one run to the next one and could be different if you use command pipelines). See setvbuf and fflush. In practice, take the habit of ending your printf format control string with \n and/or explicitly calling fflush

Many open source programs doing what you want already exist. GNU ed comes to mind. Consider studying its source code for inspiration.

Please read how to debug small programs. If you use a recent GCC compiler with some GDB debugger, compile with all warnings and debug info, so gcc -Wall -Wextra -g then use gdb to understand the behavior of your program. Specify on paper the input file syntax using EBNF and read more about parsing techniques, including recursive descent parsing.

Notice that:

   fflush(stdin);

is undefined behavior. You should fflush output streams only.

PS. You could later read about databases then consider using sqlite.

Basile Starynkevitch
  • 1
  • 16
  • 251
  • 479
1

fgets will read from the file up to and including the newline character at the end of the line. The scanf call you use to get the replacement string does not, so when you write out newline it does not contain a newline character.

Solutions include explicitly adding the newline (possibly with fputc('\n', fTemp);, or using fgets(newline, BUFFER_SIZE, stdin); instead of the scanf to read your input string.

1201ProgramAlarm
  • 30,320
  • 7
  • 40
  • 49
0

Yes I want to use

fgets(new,line,buffer_sizze,stdin);

but it just won't ask for input unless I put it inside of main().

When I put it inside of a function that I created, it won't ask for input from the user which is why I used scanf.

Is there a way to put it \n without asking the user to type \n.

Or any solution to why it's not getting input when I used fgets.

Lorale
  • 73
  • 6
0

For the people that has the same problem as me. Fgets not asking for any input. Try use getchar(). That solved my problem. For unknown reason.

Lorale
  • 73
  • 6