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.

, , No Comments

 Certainly! Below are C programs for the specified tasks using File Handling:


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


```c

#include <stdio.h>


int main() {

    FILE *file;

    char filename[100];

    char ch;

    int lines = 0;


    printf("Enter the filename: ");

    gets(filename);


    // Open the file in read mode

    file = fopen(filename, "r");


    if (file == NULL) {

        printf("File not found or unable to open.\n");

        return 1;

    }


    // Count lines in the file

    while ((ch = fgetc(file)) != EOF) {

        if (ch == '\n') {

            lines++;

        }

    }


    fclose(file);


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


    return 0;

}

```


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


```c

#include <stdio.h>


int main() {

    FILE sourceFile, tempFile;

    char filename[100];

    char tempFilename[100];

    char buffer[1000];

    int lineToDelete;


    printf("Enter the filename: ");

    gets(filename);


    printf("Enter the line number to delete: ");

    scanf("%d", &lineToDelete);


    // Open the source file in read mode

    sourceFile = fopen(filename, "r");


    if (sourceFile == NULL) {

        printf("File not found or unable to open.\n");

        return 1;

    }


    // Create a temporary file

    sprintf(tempFilename, "temp_%s", filename);

    tempFile = fopen(tempFilename, "w");


    // Copy lines to the temporary file, excluding the specified line

    int currentLine = 1;

    while (fgets(buffer, sizeof(buffer), sourceFile) != NULL) {

        if (currentLine != lineToDelete) {

            fputs(buffer, tempFile);

        }

        currentLine++;

    }


    fclose(sourceFile);

    fclose(tempFile);


    // Remove the original file and rename the temporary file

    remove(filename);

    rename(tempFilename, filename);


    printf("Line %d deleted successfully.\n", lineToDelete);


    return 0;

}

```


### (c) To copy a file to another folder with a different file-name:


```c

#include <stdio.h>


int main() {

    FILE sourceFile, destinationFile;

    char sourceFilename[100];

    char destinationFilename[100];

    char buffer[1000];

    int bytesRead;


    printf("Enter the source filename: ");

    gets(sourceFilename);


    printf("Enter the destination filename: ");

    gets(destinationFilename);


    // Open the source file in binary mode

    sourceFile = fopen(sourceFilename, "rb");


    if (sourceFile == NULL) {

        printf("Source file not found or unable to open.\n");

        return 1;

    }


    // Open the destination file in binary mode

    destinationFile = fopen(destinationFilename, "wb");


    if (destinationFile == NULL) {

        fclose(sourceFile);

        printf("Unable to create or open the destination file.\n");

        return 1;

    }


    // Copy the contents from source to destination

    while ((bytesRead = fread(buffer, 1, sizeof(buffer), sourceFile)) > 0) {

        fwrite(buffer, 1, bytesRead, destinationFile);

    }


    fclose(sourceFile);

    fclose(destinationFile);


    printf("File copied successfully to %s.\n", destinationFilename);


    return 0;

}

```


These programs demonstrate file handling concepts in C for finding the number of lines, deleting a specific line, and copying a file to another folder with a different filename. Make sure to handle errors appropriately and adjust file paths and permissions based on your system.

0 टिप्पणियाँ:

Post a Comment