Computer >> Computer tutorials >  >> Programming >> C programming

C program to remove a line from the file


A file is a physical storage location on disk and a directory is a logical path which is used to organise the files. A file exists within a directory.

The three operations that we can perform on file are as follows −

  • Open a file.
  • Process file (read, write, modify).
  • Save and close file.

Algorithm

An algorithm is given below to explain the C program to remove a line from the file.

Step 1 − Read file path and line number to remove at runtime.

Step 2 − Open file in read mode and store in source file.

Step 3 − Create and open a temporary file in write mode and store its reference in Temporary file.

Step 4 − Initialize a count = 1 to track line number.

Step 5 − Read a line from source file and store it in buffer.

Step 6 − If current line is not equal to line to remove i.e. if (line != count), then write buffer to temporary file.

Step 7 − Increment count++.

Step 8 − Repeat step 5-7 till end of source file.

Step 9 − Close both files i.e. source file and temporary file.

Step 10 − Delete our original source file.

Step 11 − Rename temporary file with source file path.

Program

Following is the C program to remove a line from the file

#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 1000
void deleteLine(FILE *src, FILE *temp, const int line);
void printFile(FILE *fptr);
int main(){
   FILE *src;
   FILE *temp;
   char ch;
   char path[100];
   int line;
   src=fopen("cprogramming.txt","w");
   printf("enter the text.press cntrl Z:\n");
   while((ch = getchar())!=EOF){
      putc(ch,src);
   }
   fclose(src);
   printf("Enter file path: ");
   scanf("%s", path);
   printf("Enter line number to remove: ");
   scanf("%d", &line);
   src = fopen(path, "r");
   temp = fopen("delete.tmp", "w");
   if (src == NULL || temp == NULL){
      printf("Unable to open file.\n");
      exit(EXIT_FAILURE);
   }
   printf("\nFile contents before removing line.\n\n");
   printFile(src);
   // Move src file pointer to beginning
   rewind(src);
   // Delete given line from file.
   deleteLine(src, temp, line);
   /* Close all open files */
   fclose(src);
   fclose(temp);
   /* Delete src file and rename temp file as src */
   remove(path);
   rename("delete.tmp", path);
   printf("\n\n\nFile contents after removing %d line.\n\n", line);
   // Open source file and print its contents
   src = fopen(path, "r");
   printFile(src);
   fclose(src);
   return 0;
}
void printFile(FILE *fptr){
   char ch;
   while((ch = fgetc(fptr)) != EOF)
   putchar(ch);
}
void deleteLine(FILE *src, FILE *temp, const int line){
   char buffer[BUFFER_SIZE];
   int count = 1;
   while ((fgets(buffer, BUFFER_SIZE, src)) != NULL){
      if (line != count)
         fputs(buffer, temp);
      count++;
   }
}

Output

When the above program is executed, it produces the following result −

enter the text.press cntrl Z:
Hi welcome to my world
This is C programming tutorial
You want to learn C programming
Subscribe the course in TutorialsPoint
^Z
Enter file path: cprogramming.txt
Enter line number to remove: 2

File contents before removing line.
Hi welcome to my world
This is C programming tutorial
You want to learn C programming
Subscribe the course in TutorialsPoint

File contents after removing 2 line.

Hi welcome to my world
You want to learn C programming
Subscribe the course in TutorialsPoint