w3schools Logo
Monday 21 May 2012
W3schools.in Developer Network
HTML tutorials
CSS tutorials
php tutorials and scripts
MySql tutorials
Learn C Programming
Operating system

c program to delete a specified line from a text file
From w3schools.in

In this program, user is asked for a filename he needs to change. User is also asked for the line number that is
to be deleted. The filename is stored in 'filename'. The file is opened and all the data is transferred to another file
except that one line the user specifies to delete.
Program: Program to delete a specific line.

#include <stdio.h>
int main() {
FILE *fp1, *fp2;
//consider 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);
//open file in read mode
fp1 = fopen(filename, "r");
c = getc(fp1);
//until the last character of file is obtained
while (c != EOF)
{
printf("%c", c);
//print current character and read next character
c = getc(fp1);
}
//rewind
rewind(fp1);
printf(" \n Enter line number of the line to be deleted:");
//accept number from user.
scanf("%d", &del_line);
//open new file in write mode
fp2 = fopen("copy.c", "w");
c = getc(fp1);
while (c != EOF) {
c = getc(fp1);
if (c == '\n')
temp++;
//except the line to be deleted
if (temp != del_line)
{
//copy all lines in file copy.c
putc(c, fp2);
}
}
//close both the files.
fclose(fp1);
fclose(fp2);
//remove original file
remove(filename);
//rename the file copy.c to original name
rename("copy.c", filename);
printf("\n The contents of file after being modified are as follows:\n");
fp1 = fopen(filename, "r");
c = getc(fp1);
while (c != EOF) {
printf("%c", c);
c = getc(fp1);
}
fclose(fp1);
return 0;
}


Output:

Enter file name:abc.txt
hi.
Hello
how are you?
I am fine
hope the same
Enter line number of the line to be deleted:4
The contents of file after being modified are as follows:
hi.
hello
how are you?
hope the same


Explanation:

In this program, user is asked for a filename that needs to be modified. Entered file name is stored in a char
array 'filename'. This file is opened in read mode using file pointer 'fp1'. Character 'c' is used to read characters
from the file and print them to the output. User is asked for the line number in the file to be deleted. The file
pointer is rewinded back and all the lines of the file except for the line to be deleted are copied into another file
"copy.c". Now "copy.c" is renamed to the original filename. The original file is opened in read mode and the
modified contents of the file are displayed on the screen.


This page was last modified on 15 Dec 2011 at 19:47:18.
Gautam Kumar
EDP Manager
http://www.connectsin.com
Related Posts
» C program to display the multiplication table of a given num
» C program to generate the Fibonacci series
» C program to print a semicolon without using a semicolon
» C program to compare two strings without using strcmp
» C program to concatenate two strings without using strcat
» C program to delete a specified line from a text file
» C program to replace a specified line in a text file
» C program to find the number of lines in a text file
» C program which asks the user for a number between 1 to 9
» C program to check whether the given string is a palindrome
» C program to check whether the given number is a palindromic
» C program to find factorial of the given number
» C program to check whether the given number is even or odd
» C program to swap two numbers using a temporary variable
» C program to swap two numbers using bitwise operators
» C program to find the greatest of three numbers
» C program to find the greatest among ten numbers
» C program to check whether the given number is a prime
» How to create pyramid in c
» Fun with C language.