delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/06/12/08:48:41

Newsgroups: comp.os.msdos.djgpp,gnu.g++.help
Subject: Re: How to clear char array buffer[]?
References: <hReV6.15706$6d5 DOT 2182887 AT news2-win DOT server DOT ntlworld DOT com>
From: Gerall Kahla <gerall AT chromebob DOT com>
Message-ID: <871yoplvje.fsf@coin.chromebob.com>
Lines: 46
User-Agent: Gnus/5.090001 (Oort Gnus v0.01) Emacs/20.7
MIME-Version: 1.0
Date: 12 Jun 2001 07:41:57 -0500
NNTP-Posting-Host: 208.191.93.169
X-Complaints-To: abuse AT swbell DOT net
X-Trace: nnrp1.sbc.net 992349596 208.191.93.169 (Tue, 12 Jun 2001 07:39:56 CDT)
NNTP-Posting-Date: Tue, 12 Jun 2001 07:39:56 CDT
Organization: SBC Internet Services
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

"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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019