www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/06/12/03:09:54

Sender: tim AT picard DOT skynet DOT be
Message-ID: <3B25BF27.CC6B5402@falconsoft.be>
Date: Tue, 12 Jun 2001 09:05:11 +0200
From: Tim Van Holder <tim DOT vanholder AT falconsoft DOT be>
Organization: Anubex N.V.
X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.2.16-3 i686)
X-Accept-Language: en, nl-BE, nl
MIME-Version: 1.0
To: Dr Joolz <julius_mong AT hotmail DOT com>, djgpp AT delorie DOT com
Subject: Re: How to clear char array buffer[]?
References: <hReV6.15706$6d5 DOT 2182887 AT news2-win DOT server DOT ntlworld DOT com>
Reply-To: djgpp AT delorie DOT com

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


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