Understanding the Goal
The function countWord reads a file, looks for a specific word inside the file, counts how many
times the word appears, and returns that count.
Now, let's go through the code line by line.
Step 1: Function Definition
int countWord(char* filename, char* word, char* line, int size)
● This line defines a function named countWord.
● It takes four inputs (parameters):
1. char* filename → The name of the file we need to read.
2. char* word → The word we are searching for inside the file.
3. char* line → A temporary space (buffer) to store each line read from the file.
4. int size → The size of the line buffer.
● The function returns an integer (int), which is the number of times the word appears
in the file.
Step 2: Open the File
FILE* fp = fopen(filename, "r");
● We use fopen to open the file with the name stored in filename.
● The "r" means we are opening the file only for reading (not writing or modifying).
Step 3: Check if the File Opened Successfully
if (fp == NULL)
{
return -1;
}
● fopen can fail if the file does not exist or if we don't have permission to read it.
● fp (the file pointer) will be NULL if fopen failed.
● If fp == NULL, the function returns -1, which tells the user that the file could not be
opened.
Step 4: Initialize a Counter
int ret = 0;
● We create a variable ret and set it to 0.
● This variable stores the count of how many times the word appears in the file.
Step 5: Read the File Line by Line
while (fgets(line, size, fp) != NULL)
● This while loop reads the file one line at a time using fgets.
● fgets(line, size, fp):
○ Reads a line from the file and stores it in line.
○ Stops reading when it reaches the end of the file.
○ Returns NULL when there are no more lines to read.
● The loop continues running as long as fgets successfully reads a line.
Step 6: Search for the Word in the Line
char* ptr = line;
● We create a pointer ptr and set it to the start of the line.
● This pointer will help us scan the line for occurrences of word.
Step 7: Find and Count the Word
while (1)
● This is an infinite loop (while(1)) that will keep running until we tell it to stop.
● Inside this loop, we will search for word inside line.
char* found = strstr(ptr, word);
● strstr(ptr, word):
○ This function searches for word inside ptr (which points to the line).
○ If word is found, it returns a pointer to where word starts in the line.
○ If word is not found, it returns NULL.
Step 8: If the Word is Not Found, Stop Searching
if (found == NULL)
{
break;
}
● If found == NULL, it means the word is not in this part of the line anymore.
● We use break; to exit the loop and move to the next line.
Step 9: Increase the Word Count
ret++;
● If found != NULL, it means we found the word.
● We increase ret by 1 to count this occurrence.
Step 10: Move the Pointer to Search for the Next Occurrence
ptr = found + strlen(word);
● strlen(word) gives the length of the word.
● found + strlen(word) moves ptr past the found word so we can search for the
next occurrence.
● This ensures that overlapping matches are handled correctly.
Step 11: Close the File and Return the Count
fclose(fp);
return ret;
● After reading the entire file, we use fclose(fp); to close the file.
● Finally, we return ret, which is the total number of times the word was found.
Summary of What Happens
1. Open the file. If it fails, return -1.
2. Read the file line by line.
3. For each line:
○ Search for the word using strstr().
○ Count every occurrence.
○ Move the pointer forward to find the next match.
4. Close the file and return the total count.
Example Walkthrough
Given a file:
hello world
hello again
hello hello
And calling:
countWord("file.txt", "hello", line, 100);
● The function reads each line.
● It finds "hello" in line 1, line 2, and twice in line 3.
● It returns 4.
Final Thoughts
● This function is a simple version of the grep command in Linux.
● It correctly handles edge cases like:
○ Words appearing multiple times in a single line.
○ Ensuring matches don’t overlap incorrectly.
If you understand this explanation, you should be ready to answer test questions about:
● File handling (fopen, fclose)
● String searching (strstr)
● Looping through a file with fgets
● Counting occurrences of a word