Jump to content

  • Log In with Google      Sign In   
  • Create Account


Text Parsing in C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Sir Derk   Members   -  Reputation: 122

Like
Likes
Like

Posted 09 August 2001 - 06:45 PM

I'm just wondering... how would u parse through text in c++? i'm pretty much a beginner here so i'm not to familiar with a lot of stuff. Umm...ok..say, for example, u have the text "hello world!" and u want to take "hello" and "world!" and put it in an array called..uhh...array... so in other words u want array[0] = "hello" and array[1] = "world!" and well generally, the seperator would be the space " ". so theoretically, the process would be for each character in the text, check if the character (being processed) is " " (a space). if it is, then store everything from that point to the last point processed to the variable array[counter] and increament the array counter by 1. next character and well in vb coding it would look somewhat like this
  
dim i as long
dim lastpos as long
dim array(1) as string  'using 1 cuz i know how many items there are in the text :)
dim arraycounter as long

lastpos = 1
for i = 1 to len(text)
 if mid(text, i, 1) = " " then
  array(arraycounter) = mid(text, lastpos, i - lastpos - 1)
  arraycounter = arraycounter + 1 '*sigh* damn vb has no ++ op =(
  lastpos = i + 1
 end if
next i
  
well i just typed that out, out of the blue....but see..now i need to convert that into c. any suggestions? Edited by - Sir Derk on August 10, 2001 1:58:12 AM

Sponsor:

#2 Sandman   Moderators   -  Reputation: 2079

Like
Likes
Like

Posted 09 August 2001 - 11:41 PM

Use strtok();

    
// Example from MSDN


#include <string.h>

#include <stdio.h>

char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;

void main( void )
{
printf( "%s\n\nTokens:\n", string );
// Establish string and get the first token:

token = strtok( string, seps );
while( token != NULL )
{
// While there are tokens in "string"

printf( " %s\n", token );
// Get next token:

token = strtok( NULL, seps );
}
}


Edited by - Sandman on August 10, 2001 6:42:48 AM

#3 Sir Derk   Members   -  Reputation: 122

Like
Likes
Like

Posted 10 August 2001 - 06:35 AM

wow thanx =) ..but...but...but...
what about searching for text? like if u want to parse through an html and sort out the tables and stuff...how would u do that?

so for things like
  
<table>
<tr>
<td>
hello
</td>
</tr>
<tr>
<td>
world
</td>
<td>
!
</td>
</tr>
</table>

^ oo..that looked terrible without the source tags ^

so...i would have to first search for the <table> tag...then the <tr>, <td>, and take the "hello" value and such.

i've already coded this out in vb..but i just dunno where it is anymore..haha i've lost the coding =)

but this one, you would only want the stuff in the tables. anything else around the table you would ignore.

initially, i was thinking, find the <table>...</table> tags and store that into a variable and use the method posted above to parse through it, using the <td>, <tr>, </td>, </tr>'s as the seperator. BUT, how would i first search for the <table>...</table> tags ignoring everything else around it?? ahh! =)

AND this method would be the same as if i would be parsing through text like

"'Hello world';txtHello:4,'wowwie';txtWow:20,'bah';txtbah,'lala';txtlala:8"

and i wouldn't be able to use seperators cuz i need em in a specific order so

"Hello World", txtHello, and 4 is stored into a umm, type structure or a class

"wowwie", txtWow, and 20 in another

"bah", txtbah in another (notice it's missing a number value?

and lastly, "lala", txtlala, and 8 in the same

so u store the first var into a text variable, the second in a name field, and the last as a value field.

type[0].text = "Hello World"
type[0].name = "txtHello"
type[0].value = 4

type[1].text = "wowwie"
type[1].name = "txtWow"
type[1].value = 20
etc...etc... (while the "bah" one does not have a value for the value field so it would become default; 0)

wow this is getting complicated...hehe... ^-^...

ok..so all in all...
i'm not really asking how to code all that...i'm just wondering what C++ commands i should use.

strtok(); seperates the text using seperators and i can't use that method for that.

hmmmm...oo i know what's a good example!! Scripting Engines!!!

oy i can't remember the one i made, it's been so long.... i don't remember how i did it ... but i remember i used to have 2 big long string variable, one for a kind of variable system (associates ids into text) and then the real speech that knows what to display. (but i've only made one so i really have no experiance in making these engines =(). hmm now i'm wondering what's the better way...but let's not get side tracked...wow i'm like writing a story here...haha..i'll just post and see what i get ^^

------
^-^ Smile ^-^

Sir Derk

Edited by - Sir Derk on August 10, 2001 1:38:47 PM

#4 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 11 August 2001 - 05:44 AM

You should probably look at regular expressions (regex) to do that.

Boost.org got a class for using regex and you can find info on how to foumulate a regular expression to find a pattern in a text file. I think there''s some example on how to highlight text using HTML statement using this class and finding HTML links in a page. You could probably figure out the way to do what you want to do checking this code.

http://www.boost.org/libs/regex/index.htm

#5 Shadowdancer   Members   -  Reputation: 319

Like
Likes
Like

Posted 11 August 2001 - 06:02 AM

Something I found about strtok() in a Linux manpage:
---
BUGS
When using this funcion note that:

These functions modifies its first argument.

The identity of the delimiting character is lost.

These functions cannot be used on constant strings.

The strtok() function uses a static buffer while
parsing, so it''s not thread safe. Use strtok_r() if
this matters to you.
---

I don''t know whether that''s true for each and every C library out there, but I''d say that it''s pretty much it for strtok(). I even remember reading something like "DON''T USE IT!" somewhere else.

#6 Anthracks   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 August 2001 - 06:28 AM

Shadowdancer, you''re right, that is (as far as I know) the standard behavior of strtok(), it works the same way in Visual C++ 6. Wish I''d noticed that note the first time I played with strtok, gave me a royal headache trying to figure out how my source string kept getting corrupted .

I wouldn''t go so far as to say NEVER USE IT though, it''s pretty handy, just always make a copy of the original string to pass to the function so it doesn''t get destroyed. The only situation I''ve heard it is really bad in is multithreaded programming; it stores some data in static variables, so if two threads were both calling strtok, they''d mess up each other''s data.

Anthracks

#7 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 11 August 2001 - 08:20 PM

well if you are loading a whole bunch of stuff from a file and it is seperated by whitespace this is all you have to do:

  

vector<string> data;//vector to store strings

/*copies strings from file into the vector,
breaking them up by whitespace*/


copy (
istream_iterator<string>(ifstream("filename")),
istream_iterator<string>(),
back_inserter(data)
);

//ok now that the file is loaded and broken up into

//words (yes in one line of code!) we want

//to find the table tag, to do that we can use

//find_if, but we need our own string to match

//assume we have other tags defined too

string tableTag("<table>");

vector<string>::iterator startPosition,endPosition;

startPosition = find_if(data.begin(),data.end(),tableTag);
//now we have an iterator to the opening tag;

endPosition = find_if(startPosition(),data.end(),tableCloseTag);
vector<string> tableData;//this will hold the table

copy(startPosition,endPosition+1,back_inserter(tableData);
//we had to add one to end because that is how iterators work

//just like how the length of an array is always one

//more than the index of the last element



I''m not going to write your whole program for you but can you see how much easier it is when using the standard library. In about eight lines of code I loaded a file, broke it apart into words and extracted a table. That''s why you should use string instead of char*, vector instead of arrays, copy with back_inserter instead of for loops, basically use C++ if you want to program in C++. This is just a very small example of what you can do with the standard library, it gets even more amazing when you get into function objects and some of the more advanced features. Pick up a book on the standard library, you won''t regret it. I have the one by Josuttis, it is pretty good.

#8 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 11 August 2001 - 08:37 PM

"''Hello world'';txtHello:4,''wowwie'';txtWow:20,''bah'';txtbah,''lala'';txtlala:8"
and i wouldn''t be able to use seperators cuz i need em in a specific order so
"Hello World", txtHello, and 4 is stored into a umm, type structure or a class
"wowwie", txtWow, and 20 in another"

Oh this is easy too. Do you recognize code like this:
  
int i;
double d;
MyStruct ms
cin >> i >> d >> ms;

wonder how that works? First let''s make one for a simple class that just has a char c followed by a float f:
  
istream& operator>> (istream& stream, MyStruct& ms)
{
stream >> ms.c >> ms.f;
return stream;//always do this so you can chain them

}//that''s it



now for more complex types (where you have to read in spaces as spaces for example, other than merely using them to seperate stuff) you need to read in a whole line using one of the stream functions and then you use string functions to break up the string and then I think you can use the input operator on a string_stream or whatever. You have to look this one up yourself. Or to keep things simple just replace all the spaces in your strings with some other character and then replace them, there is a standard function to do that easily, and it works on all containers too. Then you won''t have to worry about cutting up a whole line yourself. So in conclusion, use STL.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS