C-Programming Tutorials

C Program to Replace a Specific Line in a Text File

This C program is used to replace a specific line in a text file.

#include <stdio.h>

int main(void) {

    FILE *fp1, *fp2;

    //'filename'is a 40 character string to store filename

    char filename[40];

    char c;

    int del_line, temp = 1;

    //asks user for file name

    printf("Enter file name: ");

    //receives file name from user and stores in 'filename'

    scanf("%s", filename);

    fp1 = fopen(filename, "r");

    //open file in read mode

    c = getc(fp1);

    //print the contents of file .

    while (c != EOF) {

        printf("%c", c);

        c = getc(fp1);

    }

    //ask user for line number to be deleted.

    printf(" Enter line number to be deleted and replaced");

    scanf("%d", &del_line);

    //take fp1 to start point.

    rewind(fp1);

    //open copy.c in write mode

    fp2 = fopen("copy.c", "w");

    c = getc(fp1);

while (c != EOF) {

        if (c == '') {

            temp++;

        }

        //till the line to be deleted comes,copy the content from one file to other

        if (temp != del_line){

        putc(c, fp2);

        }

        else //when the line to be deleted comes

        {

            while ((c = getc(fp1)) != '') {

            }

            //read and skip the line ask for new text

            printf("Enter new text");

            //flush the input stream

            fflush(stdin);

            putc('', fp2);  //put '' in new file

            while ((c = getchar()) != ''){

                putc(c, fp2);

                //take the data from user and place it in new file

                fputs("

                ", fp2);

                temp++;

            }

            //continue this till EOF is encountered

            c = getc(fp1);

            }

        //close both files

        fclose(fp1);

        fclose(fp2);

        //remove original file

        remove(filename);

        //rename new file with old name opens the file in read mode

        rename("copy.c", filename);

        fp1 = fopen(filename, "r");

        //reads the character from file

        c = getc(fp1);

        //until last character of file is encountered

        while (c != EOF){

            printf("%c", c);

            //all characters are printed

            c = getc(fp1);

        }

        //close the file pointer

        fclose(fp1);

        return 0;

}

}

Program Output:

Enter file name:abc.txt

hi.

hello

how are you?

hope the same

Enter line number of the line to be deleted and replaced:4

Enter new text: sayonara see you soon

hi.

hello

how are you?

sayonara see you soon

Explanation:

In this program, the user is asked to type the name of the file. The File by name entered by user is opened in read mode. The line number of the line to be replaced is asked as input. Next the data to be replaced is asked. A new file is opened in write mode named "copy.c". Now the contents of original file are transferred into new file and the line to be modified is deleted. New data is stored in its place and remaining lines of the original file are also transferred. The copied file with modified contents is replaced with the original file's name. Both the file pointers are closed and the original file is again opened in read mode and the contents of the original file is printed as output.

 

C Program to Find the Number of Lines in a Text File (Prev Lesson)
(Next Lesson) C Program to Delete a Specific Line From a Text File
Back to C-Programming Tutorials

No Comments

Post a Reply

Course Curriculum

error: Content is protected !!