Commit fb3f53e7 by jobrod

Add new file

parents
Showing with 58 additions and 0 deletions
#include <stdio.h>
#include <string.h>
#define MAX_LINE 1024
// Function prototypes
void process_file(const char *filename);
int count_words(char *line);
int main()
{
process_file("raamat1184.txt");
return 0;
}
// Function to process the file and count words
void process_file(const char *filename)
{
FILE *file = fopen(filename, "r");
if (file == NULL)
{
printf("Error: Could not open file.\n");
return;
}
else
{
printf("File opened successfully.\n");
}
char line[MAX_LINE];
int total_words = 0;
while (fgets(line, sizeof(line), file))
{
total_words += count_words(line);
}
fclose(file);
printf("File closed successfully.\n");
printf("There are %d words in this book.\n", total_words);
}
// Function to count words in a line
int count_words(char *line)
{
int count = 0;
char *word = strtok(line, " \t\n");
while (word != NULL)
{
count++;
word = strtok(NULL, " \t\n");
}
return count;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment