Mail Archives: djgpp/2001/06/11/22:30:21
| From:  | "Dr Joolz" <julius_mong AT hotmail DOT com>
 | 
| Newsgroups:  | comp.os.msdos.djgpp,gnu.g++.help
 | 
| Subject:  | How to clear char array buffer[]?
 | 
| Lines:  | 106
 | 
| X-Priority:  | 3
 | 
| X-MSMail-Priority:  | Normal
 | 
| X-Newsreader:  | Microsoft Outlook Express 5.50.4522.1200
 | 
| X-Mimeole:  | Produced By Microsoft MimeOLE V5.50.4522.1200
 | 
| Message-ID:  | <hReV6.15706$6d5.2182887@news2-win.server.ntlworld.com>
 | 
| Date:  | Tue, 12 Jun 2001 03:09:29 +0100
 | 
| NNTP-Posting-Host:  | 213.105.24.25
 | 
| X-Complaints-To:  | abuse AT ntlworld DOT com
 | 
| X-Trace:  | news2-win.server.ntlworld.com 992311757 213.105.24.25 (Tue, 12 Jun 2001 03:09:17 BST)
 | 
| NNTP-Posting-Date:  | Tue, 12 Jun 2001 03:09:17 BST
 | 
| Organization:  | ntlworld News Service
 | 
| To:  | djgpp AT delorie DOT com
 | 
| DJ-Gateway:  | from newsgroup comp.os.msdos.djgpp
 | 
| 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...
The code in main() is:
 ifstream fin("test.txt");
 int i=1;
 char* token;
 while ( fin.getline(buffer,MAX).good() ) {
  token = strtok( buffer, seps );
  while ( token != NULL ) {
   dict.insertN(token, i);
   cout << i << token << endl;
   cout << dict;
   token = strtok( NULL, seps );
   i++;
   }
 }
...where test.txt contains:
Hello World
this is fun
...and will produce the following output:
1Hello
 [Hello] -> oo
2World
 [Hello] ->  [World] -> oo
3this
 [this] ->  [s fun] ->  [this] -> oo
4is
 [this] ->  [s] ->  [this] ->  [is] -> oo
5fun
 [this] ->  [s] ->  [this] ->  [is] ->  [fun] -> oo
 [] ->  [s] ->  [] ->  [is] ->  [fun] -> oo
...dict is a linked list and i've overloaded << to print the list items like
above. what i'm trying to do is read a line then tokenise each line with
separators like spaces, \n\t,. etc and store those words in the linked list.
(spell checker exercise) but i've tried a few different ways to define the
while condition but still does the same thing... any ideas?
But if I did this:
 // while ( fin.getline(buffer,MAX).good() ) {
  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++;
   }
  fin.getline(buffer2, MAX);
  token = strtok( buffer2, seps );
  while ( token != NULL ) {
   dict.insertN(token, i);
   cout << i << token << endl;
   cout << dict;
   token = strtok( NULL, seps );
   i++;
   }
 //}
without while loop but define 2 separate buffers it works and gives me this:
1Hello
 [Hello] -> oo
2World
 [Hello] ->  [World] -> oo
3this
 [Hello] ->  [World] ->  [this] -> oo
4is
 [Hello] ->  [World] ->  [this] ->  [is] -> oo
5fun
 [Hello] ->  [World] ->  [this] ->  [is] ->  [fun] -> oo
Any help appreciated... please help...
Jules
- Raw text -