HomeC ProgrammingC Program to Delete a Specific Line from File

C Program to Delete a Specific Line from File

This C program reads a file, deletes a specific line chosen by the user, and writes the modified content back to the file.

Problem Statement

Given a file, the program prompts the user to enter the line number to be deleted from the file. It then reads the file, removes the specified line, and updates the file with the modified content.

C Program to Delete a Specific Line from File

#include <stdio.h>
#include <stdlib.h>

void deleteLine(const char *filename, int lineToDelete) {
    // Open the input file
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        printf("Failed to open file: %s\n", filename);
        return;
    }

    // Open a temporary file for writing
    FILE *tempFile = fopen("temp.txt", "w");
    if (tempFile == NULL) {
        printf("Failed to create temporary file.\n");
        fclose(file);
        return;
    }

    // Read lines from the input file and write them to the temporary file,
    // skipping the specified line
    char buffer[1024];
    int currentLine = 1;
    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        if (currentLine != lineToDelete) {
            fputs(buffer, tempFile);
        }
        currentLine++;
    }

    // Close the files
    fclose(file);
    fclose(tempFile);

    // Delete the original file
    remove(filename);

    // Rename the temporary file to the original filename
    rename("temp.txt", filename);

    printf("Line %d deleted successfully from file: %s\n", lineToDelete, filename);
}

int main() {
    const char *filename = "example.txt";
    int lineToDelete = 3;

    deleteLine(filename, lineToDelete);

    return 0;
}

How it Works

The C program works in the following steps:

  1. The program defines a function called deleteLine that takes the filename and line number as input parameters.
  2. Inside the deleteLine function, it opens the input file in read mode using fopen and checks if the file was successfully opened. If not, it displays an error message and returns.
  3. The function then opens a temporary file in write mode using fopen to store the modified content.
  4. It uses a loop to read each line from the input file using fgets. For each line, it compares the current line number with the line number to be deleted. If they match, the line is skipped; otherwise, it is written to the temporary file using fputs.
  5. After processing all the lines, the input file and the temporary file are closed using fclose.
  6. The original file is deleted using remove.
  7. The temporary file is renamed to the original filename using rename.
  8. Finally, a success message is printed.

In the main function, you can customize the filename and the line number to delete. The program then calls the deleteLine function with the provided inputs. If the line number is invalid (negative or greater than the total number of lines in the file), the function will display an error message.

The program follows good programming practices by handling file opening errors, properly closing the files, and providing appropriate success/error messages.

Input / Output

C Program to Delete a Specific Line from File

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C program calculates the volume and surface area of a sphere using its radius. A sphere is a three-dimensional...
This C program converts a Roman numeral to a decimal number. Roman numerals are a system of numerical notation used...
This C program calculates the value of sin(x) using the Taylor series expansion. The Taylor series expansion is a mathematical...