Mail Archives: djgpp/2001/06/12/08:48:41
"Dr Joolz" <julius_mong AT hotmail DOT com> writes:
> 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...
>
How about adding a line like this:
<paste>
> while (!fin.eof()) {
// this clears the buffer before reading
memset( buffer, '\0', sizeof(buffer));
> 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++;
> }
> }
--
Gerall Kahla a.k.a the Celestial Mechanic
http://chromebob.com -- homepage
http://chromebob.com/public-key.asc -- GnuPG public key
- Raw text -