Coders Packet

File Content Details and Word Occurrence Using C++

By Anshuman Prakash

It is a C++ code that will count the total number of words, lines, and occurrences of a given word in a text file.

In this tutorial, we will use C++ to do operations like counting the total number of words and lines or counting the total number of occurrences of a word on a text file. It is totally based on file handling and iterations and pointers are not used.

This is a very simple concept and is easy to understand but might have more complexity than other concepts and methods. This can be useful in assignments and letters where there is a limit on the number of words to be written.

Ensure that this code is not case sensitive and will detect words even if they vary case-wise. And it will also give the wrong output if there are extra spaces in the file.

Methodology

Firstly all the necessary libraries were included for standard input-output, file handling, and string operations.

#include
#include //necessary library for file handling
#include //necessary library for strings

Then we will ask the user to input the path of the file to be opened and then will store the path in a string variable 'filePath'.

The next step is to open the file using the input stream 'ifstream' class to operate on files and the object 'fin' of the class associated with the file opened for input purpose. Also will use 'fin.fail()' function to ensure that it returns the phrase "File Not Found" in case if a wrong file path is provided.

cout<<"\nEnter the path of the file : ";
string filePath; //to store the path of the file given
cin>>filePath;
ifstream fin(filePath); //input stream class to operate on files.
if (fin.fail()){
   cout<<"\nFile Not Found";
}

Now we will ask the user to choose the operation to be performed and will proceed with the conditional statement if-else.

Then will use while(fin) loop so that the loop works as long as the read function inside the loop works. 'fin' actually returns the value of an error flag variable inside the class object 'fin' whose value is set to 0 when any function like read or write or open fails. Hence the loop will work for the full length of the file.

Now to count the total number of words and lines will use if statements and 'fin' object of 'ifstream' class to get characters from the file in order to iterate and compare. We have used '!fin.eof()' which is a character put at the eof 'end of the file' so that compiler runs till the end of the selected file. Then in the of total words, 'fin >> word' extracts formatted input from the file then increments the counter if the compiler detects a word and 'wordCount' gets increased by one. Also to count the total number of lines, as the compiler detects line change '\n' the 'line' variable is increased by one. And then the system prints the count after the loop ends.

if(input==1){
     string word;
     int wordCount = 0;
     int line=1; //will not count last line so initial value is 1
     char symbol; //to find symbol related to spaces and line change
     while(!fin.eof())
     {
          fin >> word;
          wordCount++;
          fin.get(symbol);
          if(symbol=='\n'){//to count no of lines i.e. whenever line is changed '\n'}
               line++;
          }
     }
     cout<<"Number Of Lines : "<<line<<"\nTotal number of words in file : " << wordCount;
     cout<<"\n\nWarning : This does not counts extra blank lines in the file.\n";
}

Now for the other case of counting occurrences of a word. First, we will take user input for the word to be searched and will store it in the character variable array 'word[30]' and a character array variable 'fileContent[200]' to assign the value from the file in order to compare. Then will use 'strcmp()' to compare the word provided with the content of the file. If the compiler finds a match, it will increase the variable 'countOccurrence' by one. And will print it after the loop ends.

else if(input==2){
     int countOccurrence=0;
     char fileContent[200];
     char word[30];

     cout<<"\nEnter the word to be counted (This counter is Case Sensitive) : ";
     cin>>word; //taking input for the word to be counte

     while(!fin.eof())
     {
         fin >> fileContent;
         if(strcmp(fileContent,word)==0){ //compares the word in string using strcmp() function and increases the counter if match found
              countOccurrence++;
         }
     }
     cout<<"\nNumber of times the word ["<<word<<"] is repeated in file : "<<countOccurrence<<"\n";
}

At the end we will close the file after the operation is performed using 'fin.close()'

Download Complete Code

Comments

No comments yet