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.
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) {
_______ ____ ________ _________ __________.
___ ________ ______ _____ ______ _____ ___ ________ ______ ____ ____ _________.
_____ ______ __________ __________ __________ ______ _____ ________ ___ ______ __________ ______.
___ ________ ________ ____ _________ __________ _________ ___ ___ ________.
___ _____ ______ ________ ___ __________ ________ _______ _______ ____ ___.
______ _____ ________ ____ ___ ___ _________ _______ _______ _________ ___.
_________ _____ ________ ____ ___ ________.
____ ______ _____ ________ _____ _____ _______ ___ ___ ____ ___.
__________ _______ __________ ________ __________.
______ ______ ______ ___ _______ ______ _________.
_______ ______ ______ _____ ______ ____ _______.
________ _______ ______ __________ _____ _________ ______ _________ ____ _____ __________ ________.
___ ___ _____ ___ ______ ___ _________ ____ ____ ________ _________.
__________ _______ ________ _____ ______ _____ _________ _____ _________ __________ ____.
_________ _________ _____ ___ ___ ___ ________ _________ _____ _________ ______ ___.
_____ _______ ________ ____ ___ __________ ____ _____ __________ ___.
________ _______ __________ __________ ________ ________ ________ _____.
_________ _________ ___ __________ _________ _________ _________.
_____ ___ _______ ____ ____ __________.
___ ____ _____ ____ ________ ____.
__________ ___ _____ ___ ________ ________.
_____ ______ __________ _________ ____ ____ ___.
______ ______ ____ _______ __________ _________ __________ _______ _______ __________.
__________ ______ _________ ______ ___ __________ ________ _____ _________ _________.
__________ _________ ____ __________ __________ ___.
_____ _________ _____ _________ _________ ________ _______ ______ _______.
____ ______ ________ ___ ___ __________ ________ _________ _____.
____ ______ _____ ________ __________ ____ ____ ____ ___ ________ _______.
__________ ________ ____ _________ __________ _______ ______.
___ ____ _______ ___ ________ _____ _______ ___.
____ ___ __________ __________ ______ _________ ________ ________ ____ _______.
_______ _______ _________ __________ _________ _____ _________ _________ _________ ____ ______ _________.
_____ ____ _______ _______ __________ _________ ____ ________.
______ _____ ____ ________ ____.
___ ______ _____ _____ ____ _____ _________ _________.
_________ ___ _______ ________ _______ ______ _________ ___ _______ __________ ____ _______.
__________ ____ __________ ________ ___ ______ _____ _________ _________.
______ __________ __________ ___ ____ _______ _______ _________.
_________ _________ _______ _______ _____ ______ _____ _________.
______ _________ _____ _________ _______ _________ _____ ________.
_________ ____ _________ ___ ________ _______ ___ ____.
______ ____ ___ _________ ________ ___.
___ _______ ________ ______ _________ ___ _____ ________.
_________ __________ ______ _________ ________ _________ ___ _________ ______ ___.
_________ ________ ____ ________ __________ ________ ________ _______ ______ ________ _________.
_____ ______ ________ ____ ________ ________.
_____ ________ _______ _____ __________ _______.
__________ _____ ____ _______ __________ ____ _______ ______ ___ _______.
______ ___ ______ ___ _________ ______ _____ _______ ______ ____.
_______ ___ ___ _________ _______ _____ _____ ____ __________.
________ _____ _______ _______ _______ ______ ____ ________ _________ ___.
__________ _________ ___ ___ ________ ___ ____.
___ _____ ________ ______ __________ _______ ____ _______ _________ ________ _________ ____.
___ ___ _________ _________ ______ __________ _____ ___ ___ _________ _____ __________.
_____ ___ _________ ___ _________ _________ ____ ____ ___ _____ __________.
______ ______ ________ _________ _________ ____.
_____ ______ _______ _________ _________ ___ ________ ______ _________ ________ ___ __________.
_____ __________ ___ ____ _______ ______ ________ ______ _______ _________.
__________ _________ _____ ____ ___ ___.
_________ __________ _______ _________ ________ ______ ________ ________ _____ ______.
____ ___ _________ ________ ________ ___ _______ ___ _________.
________ __________ _________ ______ _________ _____ _______ ______ _________ ________ ____ ___.
__________ _________ _________ ___ ________.
___ __________ _____ __________ ______ _______ _____ ______ _______.
_____ _________ ________ ______ ____ ____ _________ __________ _____ _________ _________.
___ ______ ______ ___ ______ ___.
_________ __________ ___ _____ _______ _______ ___ ___ _________.
__________ _______ ________ ______ _________ ____ _______ ________.
_______ ___ __________ _________ __________ ______ ________.
_____ _______ ____ _____ ________ ____.
______ __________ ________ ___ ______ __________ _________ ____ _________.
_________ ______ ___ ___ ___ _________ ________ __________ _____ _________.
______ ____ ________ _____ __________ ______ ____ _________ _________.
__________ __________ _____ _____ _________.
_________ ____ _____ _____ _______ __________ ___ __________ ___ ______ ________.
__________ _________ ______ __________ __________ _________ _________ _________ _______ _____.
_________ _________ __________ __________ _____ _____ _____ _____ _____ ________ ___.
_________ __________ ____ _____ ________ _________ _________.
__________ ________ _____ ___ _________ ___ _____ _____ ________ ____ __________.
______ _______ ______ _________ _____ _____ ___.
__________ _______ ___ __________ ______ _______.
_____ _______ ___ __________ __________ ____ ____ __________ __________.
__________ _________ _____ ____ _______.
_________ ________ _____ ___ ___ _______ _______ _______ ________ ________ __________.
____ _________ _______ ___ ______ ___ _____.
_____ ____ _________ _____ ____.
_________ ______ ______ _______ _____ _____ _______ ________.
____ _________ ___ _____ _________ __________ __________ ___ _______ ______ _____ _____.
____ ____ ______ ____ __________ ________ __________ __________ _______ _______ ________ _________.
_____ ______ _______ _____ ___ ________ _______ ______ _________ ____ ___ ______.
_________ ______ ______ ____ ___ _________ _____ ________ ____ ________ __________ _________.
_____ __________ _______ ______ ____ _____ __________ ______.
_____ _____ ____ _________ _________ _________.
___ __________ ________ __________ _____ _______ _____ ___ __________ _______.
___ __________ __________ _____ _________ ___ ____ _________ ______ _____ ________.
____ ________ _________ ________ _______.
__________ ___ _____ ______ _________.
__________ _______ _____ ____ ___ ____ ________ __________ ___.
_______ ___ _________ _____ ____ ___ ________.
Get Full Answer on WhatsApp