Question

Using File Handling concept in C programming, write the C programs for the following: 

(a) To find the number of lines in a text file.
(b) To delete specific line from a text file.
(c) To copy a file to another folder with a different file-name.

07 Aug 2023
Answer :
Word Count : 1150

 Here are the C programs using File Handling concepts to perform the tasks you mentioned:

(a) To find the number of lines in a text file:


#include <stdio.h>

int main() {
    FILE *file;
    char filename[100];
    char ch;
    int lineCount = 0;

    printf("Enter the name of the file: ");
    scanf("%s", filename);

    file = fopen(filename, "r");
    if (file == NULL) {
        printf("Unable to open the file.\n");
        return 1;
    }

    while ((ch = fgetc(file)) != EOF) {
        if (ch == '\n') {
            lineCount++;
        }
    }

    printf("Number of lines in the file: %d\n", lineCount + 1);

    fclose(file);

    return 0;
}
 

(b) To delete a specific line from a text file:


#include <stdio.h>

int main() {
    FILE *sourceFile, *tempFile;
    char sourceFilename[100], tempFilename[100];
    char ch;
    int lineNumber, currentLine = 1;

    printf("Enter the name of the source file: ");
    scanf("%s", sourceFilename);

    printf("Enter the line number to delete: ");
    scanf("%d", &lineNumber);

    sourceFile = fopen(sourceFilename, "r");
    if (sourceFile == NULL) {
        printf("Unable to open the source file.\n");
        return 1;
    }

    tempFile = fopen("temp.txt", "w");
    if (tempFile == NULL) {
        printf("Unable to create temporary file.\n");
        fclose(sourceFile);
        return 1;
    }

    while ((ch = fgetc(sourceFile)) != EOF) {
        if (ch == '\n') {
            currentLine++;
        }

____ ______ ______ __________ _______.
________ _____ _____ ___ ______ ______ __________.
_____ ___ _________ ________ __________ _________ _____ _______.
_________ _______ ______ ___ ________ _____ _________ ____ _____ ___.
_______ ____ _______ ________ ___ ________ ___.
_______ ________ _____ _____ _______ ________ ______ ______ _____ _____.
___ ____ ___ _____ _________ __________ _________ _____ __________ _____ ______.
___ ______ _______ _____ _____ _____.
_____ _____ _______ ___ _______ __________ _______ __________.
__________ _________ __________ ____ ______ __________ ______ ______.
_______ ______ ___ _________ ____ _______.
____ __________ _______ _________ _____ __________ ______.
_______ ______ ____ ___ _________ __________ ____ ___ __________ _______ ________.
_______ _______ ____ _______ __________ ____ ________ _____ _________ ____ _______.
___ __________ _____ __________ ___ ______ _________ ______ __________ ______ ____ _________.
_______ ________ _____ _________ ___ _____ ________ ____ ________ ____ ______.
_________ ____ ___ ____ ______ ____.
_____ _________ ____ ___ ____.
____ _______ _____ ______ _______ __________.
_________ __________ _____ ___ __________ ______ __________.
____ _________ ______ _______ ________ ______ _____ ______ ________ ___.
_______ ___ _______ ______ ____ ________ __________ _______ ___ __________.
______ __________ __________ _____ ____ ________ _____.
_____ _________ _____ ___ ______ ____ ____.
________ ____ ___ ____ __________ ____ ______ ____ _______ ______ ________ __________.
___ ____ ________ __________ _______ ___.
__________ ________ ___ __________ ________ ____ ___.
______ ___ _____ ______ __________ _______.
__________ ________ _________ _______ _________ ____ ______ _____ __________ _________.
_____ ______ ______ ____ ___ _________.
_________ _____ _____ ___ __________ ____ _______ ____ _______.
_____ _____ ________ _______ ___.
_______ ___ __________ _________ _____ ____ _________ ____ _____ ________ _________ _______.
______ __________ _________ __________ ____ _____.
_______ ____ __________ ___ ____ ______ ___ ___.
_______ __________ _______ ___ _____ _____ ___ _____ _______ ____.
__________ ______ _______ _______ _____ ______ _____ ___.
____ ___ _____ ___ ____ __________ ____ _____ ____ _______ ______ ________.
_____ __________ ________ _________ ________.
___ _____ _______ ____ ____ _______.
___ ____ ____ __________ ____.
_______ ________ ___ _________ ____ ___.
____ _____ ______ ___ _____.
________ _____ ____ _______ _________ ________ _______ ____.
_______ ________ __________ ______ ___.
_________ ______ _______ _______ ___ _______ ______ ______ ___ ___ _________.
_________ _____ ________ __________ ________ ___ ___ _________ ___.
_____ __________ _________ _________ ________ ________ ___ _____ _________ _______.
___ ______ ___ ________ _______ ___ ________.
_______ _________ ______ _________ __________ ____.
____ ________ ____ ______ ___.
_____ ______ ___ ________ ______ _______ _________ ____ __________ ___.
____ ____ ________ ___ __________ ____ _______ ___ ______.
_________ ________ ____ _______ ________ ____ ____ ____ ______ ____.
________ ___ _______ __________ _________ ___ __________ ___ ___ _____.
______ _______ ___ ____ _________ ______ ________ ___ ______ __________ ___.
________ ________ __________ ____ ____ ________ _____ ________ _______ ________ ____ _________.
_______ ____ _________ __________ _______ ______ _______ _____ __________.
________ _____ _________ ___ ________ ______ _____ ____ _________ _________.
___ _______ _______ ____ _________ ______ _________.
____ _____ __________ ________ ________.
_____ _________ ____ __________ ______ ________ __________ ____ _____ _________ _________ ________.
__________ __________ __________ _______ ________ ___ _________ _________.
___ __________ ________ ____ _________ __________ _____ ______ ___ ________.
________ ______ ________ ______ ____ ____.
___ _________ _____ ______ ___ ____ _____ ___ ____ _________ __________ ______.
_________ ________ ____ ________ ________.
_______ _______ _____ _________ _______ ____ ____ __________ ______.
_______ ________ __________ __________ ____ _________.
__________ ___ _________ ________ _____ _____.
________ _________ ________ _______ ____ ___ _______.
__________ _______ _______ ___ ______ _____ ____ ______.
________ _______ ______ ________ ___ __________ __________.
_______ ________ _________ ________ ___ _____ ____.
___ _________ _________ ______ ______ ______.
_________ ______ ___ ______ ___ _____ __________ __________ ________.
___ __________ ________ ______ ___ ________ __________ ____ _______ _______.
_______ _________ _________ __________ _________.
________ ________ ______ ____ _____ ______ ________ _______ _______ _____ _____.
______ _____ _____ _______ _______ _______ _________ __________.
_______ ______ _______ ______ __________ ________ ________ _______ ________ ____.
_______ ___ __________ _____ ________ ___ _____.
_______ __________ _______ ____ ___ _____ _____ _____ ________ ____.
_________ _______ ______ _____ ____.
___ ______ _____ ________ _________ ________ ____.
_______ _____ ________ ______ _____ _____ __________ _________ ________.
__________ _______ __________ ______ ______ ________ __________ _________ ______ _____ _________ _________.
______ __________ _________ _______ ____ _________ ________ ________.
______ ____ ___ __________ ________ ______ _____ ____ ______ ___ __________.
______ ______ __________ _________ ___ _________ ____ _______ ________.
_____ ___ _________ ___ ____ ____ __________ ________ _______ _____ _____ ____.
________ ________ ______ ____ _______ _________ ______ _____ ____ ______ ____.
____ __________ _________ _________ _________ _____ ________.
____ ______ ____ ___ ______ _________ ____ _____ _________ ___ __________ ________.
___ _________ ________ ________ ________ _________.
________ __________ _______ ____ ________ _________ __________ __________ _________.
_______ ______ ________ _________ _____ ______.
___ ____ _________ ___ _______ _________ _________ ___.
IGNOU ACADEMY
Your IGNOU journey, made easier.
  • Solved Assignments
  • Study Notes
  • Guess Papers
  • Question Papers
Skip in 10
Top
📞
Call Support Instant phone assistance IGNOU MCS 11 2023 2024 - Using File Handling concept in C programming, write the C programs for | IGNOU Academy
→
🟢
WhatsApp Chat Fast live messaging
→
Email Us Business enquiries & support
→