1

I posted this over at Code Review Beta but noticed that there is much less activity there.

I have the following code and it works just fine. It's function is to grab the input from a file and display it out (to confirm that it's been grabbed). My task is to write a program that counts how many times a certain word (string) "abc" is found in the input file.

Is it better to store the input as a string or in arrays/vectors and have each line be stored separately? a[1], a[2] ect? Perhaps someone could also point me to a resource that I can use to learn how to filter through the input data.

Thanks.

input_file.open ("in.dat");
while(!input_file.eof()) // Inputs all the lines until the end of file (eof).
{
    getline(input_file,STRING); // Saves the input_file in STRING.
    cout<<STRING; // Prints our STRING.
}
input_file.close();
Daniel Imms
  • 43,032
  • 14
  • 130
  • 152
  • 4
    Where does everyone get the idea the `.eof()` as a loop condition is a good idea? [It is not.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – us2012 Feb 25 '13 at 03:47
  • 2
    @us2012 it's taught in my textbook... –  Feb 25 '13 at 03:49
  • Click the link in my comment to find out why you should not do it :) – us2012 Feb 25 '13 at 03:49
  • Thank you, I didn't notice the link. I appreciate it... –  Feb 25 '13 at 03:51

1 Answers1

0

Reading as much of the file into memory is always more efficient than reading one letter or text line at a time. Disk drives take a lot of time to spin up and relocate to a sector. However, your program will run faster if you can minimize the number of reads from the file.

Memory is fast to search.

My recommendation is to read the entire file, or as much as you can into memory, then search the memory for a "word". Remember, that in English, words can have hyphens,'-', and single quotes, "don't". Word recognition may become more difficult if it is split across a line or you include abbreviations (with periods).

Good luck.

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144