Mail Archives: djgpp/2001/06/12/03:09:54
> Dear all, I have some code like this:
>
> while (!fin.eof()) {
> fin.getline(buffer, MAX);
> token = strtok( buffer, seps );
> while ( token != NULL ) {
> dict.insertN(token, i);
> cout << i << token << endl;
> cout << dict;
> token = strtok( NULL, seps );
> i++;
> }
> }
>
> and it is causing problems with what's read in buffer every time getline is
> executed, if I used a different char buffer in getline in each while exec
> problem gone. So I know it is buffer that's not being reset to empty at the
> end of each while loop, how do I do that? I tried buffer = ""; or NULL or
> assigning buffer[i] = '\0' etc won't work...
You never show the declaration of your buffer, but I assume it'll be
something like
char buffer[MAX];
If this is the case, simply use memset() to clear the buffer; simply
adding
memset (buffer, 0, sizeof buffer);
at the end of your outer while loop should do it.
IIRC, the >> operator skips whitespace by default; since in your example
you're tokenizing using whitespace, you could simply use something like
ifstream fin(blah);
string token;
while (!fin.eof ()) {
fin >> token;
// blah
}
--
Tim Van Holder - Anubex N.V.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
This message was posted using plain text. I do not endorse any
products or services that may be hyperlinked to this message.
- Raw text -