| View previous topic :: View next topic |
| Author |
Message |
NothingToShow Grandmaster Cheater Supreme
Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Wed Jul 15, 2009 5:55 am Post subject: [C] Split/Parse info out of string/char? |
|
|
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 |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Jul 15, 2009 7:18 am Post subject: |
|
|
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 |
|
 |
NothingToShow Grandmaster Cheater Supreme
Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Wed Jul 15, 2009 8:08 am Post subject: |
|
|
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 |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Wed Jul 15, 2009 10:29 am Post subject: |
|
|
| unsigned char searchingText[] = "username\"";
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Wed Jul 15, 2009 11:40 am Post subject: |
|
|
Tokens
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
SenHu How do I cheat?
Reputation: 0
Joined: 27 Jul 2009 Posts: 1
|
Posted: Fri Aug 14, 2009 10:08 am Post subject: Extracting XML values |
|
|
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 |
|
 |
&Vage Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Jul 2008 Posts: 1053
|
Posted: Fri Aug 14, 2009 2:39 pm Post subject: |
|
|
| 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 |
|
 |
kinghamza How do I cheat?
Reputation: 0
Joined: 14 Aug 2009 Posts: 5
|
Posted: Fri Aug 14, 2009 7:15 pm Post subject: |
|
|
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 |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Fri Aug 14, 2009 8:55 pm Post subject: |
|
|
| 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 |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Sat Aug 15, 2009 6:42 am Post subject: |
|
|
would he just want to use the " delimiter?
_________________
|
|
| Back to top |
|
 |
|