Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with strtok()

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm trying to read in a large file, after is read, I have to get rid of anything that is not a word and then count the number of words. The first part I tried to do with strtok(), I read in the file using getline() so that it reads it one line at a time, then I send that input into the tokenizer. After that, I send the "filtered" string to an output file.
For some reason, my output file has repeated lines of text. There are some lines that don't even get tokenized! can anyone help?

Here's what I have so far...

Code:
#include <fstream.h>
#include <cstring>
#include <iostream>
char*ptr;
using std::endl;



void main(void)
{
	fstream NameFile,outFile;
	char Input[81];

	
	NameFile.open(&quot;arm95.txt&quot;, ios::in);
	if (!NameFile)
	{
		cout << &quot;File open error!&quot; << endl;
		return;
	}
	outFile.open(&quot;arm95output.txt&quot;, ios::out);

	while (!NameFile.eof())
	{
		NameFile.getline(Input,81); // use \n as a delimiter
	
		ptr = strtok(Input, &quot;1234567890!@#$%^&*()_+-=;:',.<>|}{[]`~&quot;);
		while (ptr != NULL){
			cout << ptr << endl;
			ptr = strtok(NULL,&quot; &quot;);
			outFile << Input << endl;}
		//cout << Input << endl;
	}
	NameFile.close();
	outFile.close();
}

 
I'm not too familiar with strtok(), but it looks like you're sending &quot;input&quot; to the outfile, and not &quot;ptr&quot;, and you're outputing the tokenized strings to &quot;ptr&quot;

outFile << Input << endl;

Shouldn't that be this:

outFile << ptr << endl;

hope this helps, Rob
&quot;Programming is like art...It makes me feel like chopping my ear off.&quot;

- Currently down
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top