Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a File

Status
Not open for further replies.

msmiley

Programmer
Jan 31, 2002
4
US
I have a file that I need to read and determine how many times a certain string appears. Is this possible? If so, how do I do it. The file is a plain text file in wordpad.

Thanks!! :)
 
I would go along the path of opening it with CFile and reading the whole thing into a buffer. (you can get the file size with CFileStatus or CStatus (cant remember acutall name)) Read it into a buffer and use strstr on it.


char* ptr = strstr(buffer,search_string);
int counter = 0;

while(ptr)
{
counter++;
ptr = strstr(ptr+1,search_string);
}

cout<<&quot;value occurs &quot;<<counter<<&quot; times&quot;;


Something like that could do it I think.

Matt
 
So do I set the search_string to whatever I want to search?
 
yes... but it will be case sensitive... If that is a problem, convert the buffer with a CString::MakeUpper or MakeLower funciton.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top