C Program to Delete a Specific Line from File

This is a C Program to delete a specific line from a text file.

Problem Description

This C Program deletes a specific line from a text file.

Problem Solution

Take input from the user and performs delete operations in text file as shown in the program below.

Program/Source Code

Here is source code of the C Program to delete a specific line from a text file. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program Delete a specific Line from a Text File
  3.  */
  4. #include <stdio.h>
  5.  
  6. int main()
  7. {
  8.     FILE *fileptr1, *fileptr2;
  9.     char filename[40];
  10.     char ch;
  11.     int delete_line, temp = 1;
  12.  
  13.     printf("Enter file name: ");
  14.     scanf("%s", filename);
  15.     //open file in read mode
  16.     fileptr1 = fopen(filename, "r");
  17.     ch = getc(fileptr1);
  18.  `  while (ch != EOF)
  19.     {
  20.         printf("%c", ch);
  21.         ch = getc(fileptr1);
  22.     }
  23.     //rewind
  24.     rewind(fileptr1);
  25.     printf(" \n Enter line number of the line to be deleted:");
  26.     scanf("%d", &delete_line);
  27.     //open new file in write mode
  28.     fileptr2 = fopen("replica.c", "w");
  29.     ch = getc(fileptr1);
  30.     while (ch != EOF)
  31.     {
  32.         ch = getc(fileptr1);
  33.         if (ch == '\n')
  34.             temp++;
  35.             //except the line to be deleted
  36.             if (temp != delete_line)
  37.             {
  38.                 //copy all lines in file replica.c
  39.                 putc(ch, fileptr2);
  40.             }
  41.     }
  42.     fclose(fileptr1);
  43.     fclose(fileptr2);
  44.     remove(filename);
  45.     //rename the file replica.c to original name
  46.     rename("replica.c", filename);
  47.     printf("\n The contents of file after being modified are as follows:\n");
  48.     fileptr1 = fopen(filename, "r");
  49.     ch = getc(fileptr1);
  50.     while (ch != EOF)
  51.     {
  52.         printf("%c", ch);
  53.         ch = getc(fileptr1);
  54.     }
  55.     fclose(fileptr1);
  56.     return 0;
  57. }
Program Explanation

This C Program, we are reading file name using ‘filename’ variable. Using ‘fileptr1’ variable Open the file in read mode. While loop is used to print the number of characters present in the file.

advertisement
advertisement

Then rewind() function is used to set the file position to the beginning of the file of the given stream. Enter the line number of the line to be deleted using ‘delete_line’ variable.

Then ‘fileptr2’ variable is used to open the new file in write mode. While loop is used to print the number of characters present in the file. Is condition statement is used to copy except the line to be deleted. The file.Putc() function is used to copy all lines in file replica.c.

Then close the files and rename the file replica.c to original name. Using while loop print the contents of the file after being modified.

Runtime Test Cases
 
$ cc pgm47.c
$ a.out
Enter file name: pgm1.c
/*
 * C PROGRAM TO CONVERSION FROM Decimal to hexadecimal
 */
 
#include<stdio.h>
int main()
{
    long int decimalnum, remainder, quotient;
    int i = 1, j, temp;
    char hexadecimalnum[100];
 
    printf("Enter any decimal number: ");
    scanf("%ld", &decimalnum);
 
    quotient = decimalnum;
 
    while (quotient != 0)
    {
        temp = quotient % 16;
        //To convert integer into character
        if (temp < 10)
            temp = temp + 48;
        else
            temp = temp + 55;
 
        hexadecimalnum[i++] = temp;
        quotient = quotient / 16;
   }
 
    printf("Equivalent hexadecimal value of decimal number %d: ", decimalnum);
    for (j = i - 1; j > 0; j--)
        printf("%c", hexadecimalnum[j]);
    return 0;
}
 
 
 Enter line number of the line to be deleted: 10
 
 The contents of file after being modified are as follows:
*
 * C PROGRAM TO CONVERSION FROM Decimal to hexadecimal
 */
 
#include<stdio.h>
int main()
{
    long int decimalnum, remainder, quotient;
    int i = 1, j, temp;
 
    printf("Enter any decimal number: ");
    scanf("%ld", &decimalnum);
 
    quotient = decimalnum;
 
    while (quotient != 0)
    {
        temp = quotient % 16;
        //To convert integer into character
        if (temp < 10)
            temp = temp + 48;
        else
            temp = temp + 55;
 
        hexadecimalnum[i++] = temp;
        quotient = quotient / 16;
   }
 
    printf("Equivalent hexadecimal value of decimal number %d: ", decimalnum);
    for (j = i - 1; j > 0; j--)
        printf("%c", hexadecimalnum[j]);
    return 0;
}

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at other example programs on File Handling, go to File Handling. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.