Hi,
I am trying to read a txt file. And make a count on how many times certain string appear in the file. But i am not sure how to break the whole line of the text into each individual string to compare it. Help.
Here is what i have for now:
// reading a text file
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int main ()
{
char buffer[2000];
//char str="test";
int count;
ifstream inputfile ("testing.txt");
count=0;
if (! inputfile.is_open())
{ cout << "Error opening file"; exit (1); }
while (! inputfile.eof() )
{
inputfile.getline (buffer,2000);
if (buffer.contains("test"))
count=count+1;
cout << buffer << endl;
cout << "count = " << count << endl;
}
return 0;
}
I want to see how many time the string "test" appear in testing.txt
Raph
I am trying to read a txt file. And make a count on how many times certain string appear in the file. But i am not sure how to break the whole line of the text into each individual string to compare it. Help.
Here is what i have for now:
// reading a text file
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int main ()
{
char buffer[2000];
//char str="test";
int count;
ifstream inputfile ("testing.txt");
count=0;
if (! inputfile.is_open())
{ cout << "Error opening file"; exit (1); }
while (! inputfile.eof() )
{
inputfile.getline (buffer,2000);
if (buffer.contains("test"))
count=count+1;
cout << buffer << endl;
cout << "count = " << count << endl;
}
return 0;
}
I want to see how many time the string "test" appear in testing.txt
Raph