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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading/parsing file content in an easier way

Status
Not open for further replies.

bobo12

Programmer
Dec 26, 2004
74
US
hi ppl,
i am used to using StringTokenizer in Java to help me parse for certain phrases and look for certain substrings within contents of file. I was wondering if there are built in classes in c that will allow me to do the same thing.

if not in c, what about c++?

suppose I want to count the # of times a particular client downloaded a certain file in web logs. this is just an expl to give u a flavor of what i would like to do.
 
Code:
char *strtok( char *strToken, const char *strDelimit );
All of these functions return a pointer to the next token found in strToken.
The strtok function finds the next token in strToken. The set of characters in strDelimit specifies possible delimiters of the token to be found in strToken on the current call. wcstok is a wide-character version of strtok. The arguments and return value of wcstok are wide-character strings. These functions behave identically otherwise.
 
Code:
#include <vector>
#include <string>
[red]...[/red]
vector<string> Tokenize(const & string s,const & char p)
{
  vector<string> v;
  string add;
  for(int i = 0; s.size() > i; ++i)
  {
     if(s[i] == p)
     {
        v.push_back(add);
        add = "";
     }
     else
     {
        add += s[i]
     }
  }
  return v;
}

I have refinded this a few times since the version posted but my tokenizers generally look like this. I'm told that the Boost library has a great tokenizing algo, but I've never used it. strtok is the only "built in" tokenizer in C/C++ at the moment -- but it lacks a few features I sometimes need (like the ability to visit only tokens, in any order).
 
I'd also recommend using Boost.

To Salem's suggestion of using Boost's String Algorithms library, I'd like to add that you might also find the Boost Tokenizer library useful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top