Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C] Split/Parse info out of string/char?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Wed Jul 15, 2009 5:55 am    Post subject: [C] Split/Parse info out of string/char? Reply with quote

Can anybody tell me, or learn me, how to parse a char/string in C? I need to parse out various information in a text file like this:
Quote:
<user_info updated_at="Mon Jun 29 08:34:07 UTC 2009" military_rank="Commander General" country="Unknown" username="DreamLine" rank="4823" id="208126"/>

Yes, it would be a lot easier to do it using XML parsers, but I'd rather do the simple method and parse it out that way.
I'd like to parse out for an example: username, military_rank and rank.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Jul 15, 2009 7:18 am    Post subject: Reply with quote

dumb way. get pointer to string. add length of first non-data bit, ie. '<user_info.....at="' then use a for loop ti find next ". the bit in between is your string.

smarter way. declare your data types (country, username, etc.) in an array of strings. go through the array with a substring search algorithm. then again, search to next "
Back to top
View user's profile Send private message
NothingToShow
Grandmaster Cheater Supreme
Reputation: 0

Joined: 11 Jul 2007
Posts: 1579

PostPosted: Wed Jul 15, 2009 8:08 am    Post subject: Reply with quote

Slugsnack: I'll work a bit with that, I've got 1 problem though:
How would you define a char/string containg " in it?
Like:
unsigned char searchingText[] = "username"";
That 'causes me problems.
Back to top
View user's profile Send private message
HolyBlah
Master Cheater
Reputation: 2

Joined: 24 Aug 2007
Posts: 446

PostPosted: Wed Jul 15, 2009 10:29 am    Post subject: Reply with quote

unsigned char searchingText[] = "username\"";
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Jul 15, 2009 11:33 am    Post subject: Reply with quote

you are talking about escape sequences

http://msdn.microsoft.com/en-us/library/h21280bw(VS.80).aspx
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Wed Jul 15, 2009 11:40 am    Post subject: Reply with quote

Tokens
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
SenHu
How do I cheat?
Reputation: 0

Joined: 27 Jul 2009
Posts: 1

PostPosted: Fri Aug 14, 2009 10:08 am    Post subject: Extracting XML values Reply with quote

Just as an alternate solution -

The following script will be better (and more general) than hard-coded c program.


Code:
# Script field.txt
# Input argument file - input file
var string file
# Input argument fieldname - name of the field
var str fieldname
# Read input into a string variable.
var string input ; cat $file > $input
# Extract field, it is between $fieldname=" and " .
var str fieldvalue
stex -r -c ("^"+$fieldname+"=\"&\"^") $input > $fieldvalue
stex -c ("^"+$fieldname+"=\"^]") $fieldvalue > null
stex "[^\"^" $fieldvalue > null
# $fieldvalue now has the value. Print it.
echo $fieldvalue



Script is in biterscripting . To try, save the script as C:\Scripts\field.txt, start biterscripting, enter the following commands one by one. (I am assuming your input file is at C:\file.txt.

script field.txt file("C:\file.txt") fieldname("username")
script field.txt file("C:\file.txt") fieldname("military_rank")
script field.txt file("C:\file.txt") fieldname("rank")


You can put all these commands in another script and execute that script instead. To call it from C code,

Code:
system("C:\biterScripting\biterScripting.exe field.txt file(\"C:\file.txt\") fieldname(\"username\") -o out.txt") ;


Output will be available in file out.txt. You can also translate this same algorithm in C code. My students often find that first thinking of a solution in terms of biterscripting commands, helps them find an algorithm, which they can translate to a lanaguage of their choice.

I would make one improvement in this script. The input file is read each time. Instead, read it only once into a string variable, then pass that string as the value for variable $input in the script field.txt.

Sen
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Fri Aug 14, 2009 2:39 pm    Post subject: Reply with quote

Code:
   char * szString = "<user_info updated_at=\"Mon Jun 29 08:34:07 UTC 2009\" military_rank=\"Commander General\" country=\"Unknown\" username=\"DreamLine\" rank=\"4823\" id=\"208126\"/>";

   char * userInfoUpdated = strtok(szString, "user_info updated_at=");
   char * militaryRank = strtok(NULL, "military_rank=");
   char * country = strtok(NULL, "country=");
        ..... and so on.
Back to top
View user's profile Send private message
kinghamza
How do I cheat?
Reputation: 0

Joined: 14 Aug 2009
Posts: 5

PostPosted: Fri Aug 14, 2009 7:15 pm    Post subject: Reply with quote

this thread is exactly what i'm looking for.

but
@void
i added a printf in your code and compiled it
Code:

printf( "user_info updated_at=%s\nmilitaryRank=%s\ncountry=%s", userInfoUpdated, militaryRank, country );


the output was
Code:

user_info updated_at=<
militaryRank=se
country=_i


o_O delimiters?
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Fri Aug 14, 2009 8:55 pm    Post subject: Reply with quote

kinghamza wrote:
this thread is exactly what i'm looking for.

but
@void
i added a printf in your code and compiled it
Code:

printf( "user_info updated_at=%s\nmilitaryRank=%s\ncountry=%s", userInfoUpdated, militaryRank, country );


the output was
Code:

user_info updated_at=<
militaryRank=se
country=_i


o_O delimiters?


He doesn't know how strtok() works, just ignore him, he's a tad stupid. It's what most of this forum does.

Anyways, the answer is strtok(). You just need to learn what the 2nd parameter is actually for. You could also use the safe version too, if you so desired.
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Sat Aug 15, 2009 6:42 am    Post subject: Reply with quote

would he just want to use the " delimiter?
_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites