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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

line parsing

Status
Not open for further replies.

ToddT

Programmer
Feb 7, 2003
14
0
0
US
I am writing an assembler and my getWord function is not working too well. Does any one know of a function that can be used to parse a string for individual words or am I just stuck with fixing my code?
 
use strspn, for more information see

Ion Filipski
1c.bmp
 
strtok could help you too.

Code:
	// Vector for safe (de)allocation of char* that might hold \0:s
	std::vector tmpBuffer;
	tmpBuffer.reserve(strlen(inputString)+1);
	char *szBuffer = &tmpBuffer[0];
	strcpy(szBuffer, inputString);
	const char* token = strtok(szBuffer, " \t\r\n");
	while (token!=0)
	{
		// do something with token...

		token = strtok(0," \t\r\n");
	}

/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
The boost tokenizer class is an excellent library made readily available that can accomplish this:


Also, this code may be useful:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//...
Code:
string file,line,word;
ifstream in(&quot;words.txt&quot;);
while(in >> word)
    // do something with word
 
I just fixed my own code, thanks for the suggestions but none would work. I needed a function that would distinguish the words when seperated by punctuation, like
word1,word2;word3 ;done
 
The strtok() example provided by PerFnurt is working.
Only you have to defile the punctuation that separes the words:
const char * sDelimiters = &quot; ,;\t:!?&quot;;
const char* token = strtok(szBuffer, sDelimiters )
while (token!=NULL)
{
// do something with token...

token = strtok(NULL, sDelimiters );
}
-obislavu-
 
>ToddT (Programmer) Feb 19, 2004
>I just fixed my own code, thanks for the suggestions but >none would work.

All of them works. Only you hsould do, is to use them. If you don't knwo how to use it does not mean yet what functions does not work.

If someone point you with the finger to the moon, look at the moon, not at the finger.
Bruce Lee.

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top