Complementary task for topic: 9
M Nemeth · 2023-08-29 15:21:04.630218'
File Handling: read, count and write
File Handling: read, count and write
Write a C program that reads a text file named "input.txt" and counts the number of lines, words, and characters in the file. The program should then write these counts to a new text file named "output.txt".
You can create a text file by copy this to notepad and save:
Hello, this is a sample text file.
It contains multiple lines and words.
Hint: fopen() opens the file, there are two arguments needed, the path (where it is, or in the same directory as your program just the filename) and the mode that can be "r"-read "w"-write "a"-append (for text based files we use these). You should know "rb","wb" are for binary files, these are not portable!
fopen will create a FILE*, a pointer points to the file itself. If returns with NULL the open did not succeed.
Other functions are related to the standard IO functions:
fgetc(FILE*)->getchar() (reads a character)
fscanf(File*,...)->scanf()
fprintf(File*,)->printf()
Before you terminate your program you should use fclose(FILE*)!
There is a special character, EOF (end-of-file), that is like '\0' in strings.
Solution
#include
int main() {
FILE *inputFile, *outputFile;
char ch;
int lines = 0, words = 0, characters = 0;
int inWord = 0; // Flag to check if we are inside a word
// Open the input file in read mode
inputFile = fopen("input.txt", "r");
// Check if the file was opened successfully
if (inputFile == NULL) {
printf("Error opening the input file.\n");
return 1;
}
// Read the input file character by character
while ((ch = fgetc(inputFile)) != EOF) {
characters++;
// Check if the character is a newline to count lines
if (ch == '\n') {
lines++;
}
// Check if the character is a whitespace to count words
if (ch == ' ' || ch == '\n' || ch == '\t') {
inWord = 0; // Not inside a word
} else if (inWord == 0) {
inWord = 1; // Inside a new word
words++;
}
}
// Close the input file
fclose(inputFile);
// Open the output file in write mode
outputFile = fopen("output.txt", "w");
// Check if the file was opened successfully
if (outputFile == NULL) {
printf("Error opening the output file.\n");
return 1;
}
// Write the counts to the output file
fprintf(outputFile, "Lines: %d\nWords: %d\nCharacters: %d\n", lines, words, characters);
// Close the output file
fclose(outputFile);
printf("Counts have been written to output.txt.\n");
return 0;
}
Explanation
The program starts by including the necessary header file stdio.h. We declare two FILE pointers, inputFile and outputFile, to handle the input and output files. We declare variables lines, words, and characters to store the counts. We initialize a flag inWord to check if we are currently inside a word. We open the "input.txt" file in read mode using fopen. If the file doesn't exist or cannot be opened, an error message is displayed, and the program returns with an error code. We read the input file character by character using fgetc, and we count the total number of characters. We also count the number of lines and words based on the occurrence of newline and whitespace characters. After reading the file, we close the input file using fclose. We open the "output.txt" file in write mode using fopen. If the file cannot be opened, an error message is displayed, and the program returns with an error code. We write the counts of lines, words, and characters to the "output.txt" file using fprintf. After writing to the file, we close the output file using fclose. Finally, we print a message indicating that the counts have been written to "output.txt".