I am trying to create a lexicon using an input file in C++

1.1k Views Asked by At

I have a file.txt I want to create a function in C++ that can read the words in that file, and print each word and how many times they occur into a file2.txt

I am doing some research I know i can use the parser and writer, and also the map class, any help please?

bool Parser::hasMoreTokens() {
  if(source.peek()!=NULL){
    return true;
}
else{
     return false;
}
}
3

There are 3 best solutions below

0
On BEST ANSWER

Is this home work? Look online for std::map, std:string, std::ifstream and std::ofstream.

0
On

If you want to be efficient and accomplish this in less possible amount of lines, but by using standard library, try this:

#include <fstream>
#include <iostream>
#include <iterator>
#include <set>
#include <string>

int main()
{
     std::ifstream f("text.txt");
     std::istream_iterator<std::string> eof;
     std::multiset<std::string> words( std::istream_iterator<std::string>(f) , eof);

     std::ofstream f_out("text_output.txt");
     for( std::multiset<std::string>::iterator i = words.begin(); i!=words.end(); i = words.upper_bound(*i) )
          f_out << "the word " << *i << " found " << words.count(*i) << " times\n";

}

This code grabs the file named "text.txt" and outputs result to "text_output.txt"

  • Contents of "text.txt":
    can can can I I I do this properly?
    How many times do I need to program one thing to remember it?

  • Contents of "text_output.txt":
    the word How found 1 times
    the word I found 4 times
    the word can found 3 times
    the word do found 2 times
    the word it? found 1 times
    the word many found 1 times
    the word need found 1 times
    the word one found 1 times
    the word program found 1 times
    the word properly? found 1 times
    the word remember found 1 times
    the word thing found 1 times
    the word this found 1 times
    the word times found 1 times
    the word to found 2 times

Good resource to learn fantastic ways of using c++ efficiently is a book called Accelerated c++.

Regards,

11
On

Read the file using ifstream, store into map of strings, use int as value of map and increment that each time you encounter a specific string. Next, write them to file using ofstream.