|
This thread is locked; no one can reply to it. |
1
2
|
Trying to parse a simple text file |
Ricardo Santos
Member #6,609
November 2005
|
Hi people I'm getting mad trying to parse a simple text file to get a couple of vars!! Here's the content of tiles.txt: # Test file ############################################################ 023332 egg 1 an 1 grass 1 - 28 shore 0 the 29 shore 0 the #------------------------------------- break! :D 300 shore 0 the The lines that are not commented have a TAB char separating the 'vars'. Now, here's my test parser:
And the output.. the number is ok, but everything else is messed! > Num: 023332 > Name: egg 1 an. > Num: 1 > Name: grass 1. > Num: 28 > Name: shore 0 . > Num: 29 > Name: shore 0 . > Num: 300 > Name: shore 0 t. Press any key to continue . . .
|
Physics Dave
Member #4,427
March 2004
|
line.substr(k,i-k); |
Ricardo Santos
Member #6,609
November 2005
|
So simple, damn! Thanks a lot! [edit] Well i 've come into another problem.. how can i convert a std::string to int? [/edit] |
Mokkan
Member #4,355
February 2004
|
|
Indeterminatus
Member #737
November 2000
|
Just as an addition to the already proposed (correct) solution by Mokkan. The following piece of code works without the use of C library functions (for the case you're interested):
_______________________________ |
Simon Parzer
Member #3,330
March 2003
|
A J
Member #3,025
December 2002
|
use XML. ___________________________ |
Mika Halttunen
Member #760
November 2000
|
adhrymes
Member #6,567
November 2005
|
this is how i did something similar in a project , should be easily adapted
|
BAF
Member #2,981
December 2002
|
Not very safe, what happens when the char function is fed something NaN? It will reach the end of a non void function, which isn't good. |
Peter Hull
Member #1,136
March 2001
|
How about using strstream?
Pete
|
adhrymes
Member #6,567
November 2005
|
yea the code isn't safe cause i wrote it hastily just then, but it can be easily fixed, i just wanted to illistrate the idea. its really a simple algorithm to do str->num if you just keep in mind what i said above about how we represent numbers base b. again we represent number base 10 so: you can just throw another else there to deal with error checking in the charToInt func and another check in the strToInt to make sure the size is valid but as i said i just wanted to illustrate the algorithm heres a more general algorithm: define a counter, set to 0 again this is not perfect since you can't cast the char to int directly but whatever just write a func for it (like i posted before or however you like) and include error checking (which i was too lazy to type in) QED |
BAF
Member #2,981
December 2002
|
And you might want to add support for floats too, which strtof has. Wouldn't be too hard though, just have it recognize '.' then change the algo after that. |
Rick
Member #3,572
June 2003
|
Quote: Don't. Using XML for something as simple as that is an overkill, IMHO I would say you either don't know much about xml or you don't like it cause it doesn't matter how simple something is xml is fast and easy. With tinyXML all you do is add 3 files to your project. No outside libraries that people have to download, plus everything is so easy. If you don't use this standard you will find yourself writing a text parser for every program/game you create, cause you will want them all in different formats. Why do that when xml will do what you want in a lot less time. That's my view on it. XML is so simple that it can never be seen as overkill. ======================================================== |
Jonatan Hedborg
Member #4,886
July 2004
|
Call me crazy but i like to use sscanf for these things
|
Thomas Fjellstrom
Member #476
June 2000
|
Sorry, but XML is not the panacea you think it is. Yes, for this, XML is deffinitely overkill. Does he need xml+xsl+xpath+dom+xlink+xblah+xfoo? Then he doesn't need xml. XML has its uses, this isn't one of them. Quote: XML is so simple that it can never be seen as overkill. Really? Have you read the XML spec? What you think is xml, is hardly even a fraction of what XML truly is. -- |
Steve++
Member #1,816
January 2002
|
Quote: What you think is xml, is hardly even a fraction of what XML truly is. The same can be said for C++, Java, HTML, etc. but I don't see that stopping anyone. Some simple XML is useful even for the little things, such as this. That's where tinyXML is handy - it's not even a library. Chances are, if he's parsing one file in his game project, he will add even more, with different formats. Unifying them into XML-based formats would certainly be helpful. |
Thomas Fjellstrom
Member #476
June 2000
|
-- |
Mika Halttunen
Member #760
November 2000
|
Quote: I would say you either don't know much about xml or you don't like it cause it doesn't matter how simple something is xml is fast and easy. With tinyXML all you do is add 3 files to your project. I'm not talking about how difficult or easy it would be to implement XML, I'm talking about the file format he's using: # Test file ############################################################ 023332 egg 1 an 1 grass 1 - 28 shore 0 the 29 shore 0 the #------------------------------------- break! :D 300 shore 0 the Now that is simple. Add some XML tags and it's not simple any more. Overkill for this purpose, as I said. |
Rick
Member #3,572
June 2003
|
Quote: What you think is xml, is hardly even a fraction of what XML truly is. That doesn't even matter, which is the great thing about XML. You don't have to use it to it's fullest. I think Steve++'s example about C++ is dead on. I don't have to use classes with C++. I don't have to use operator overloading if I don't need it, but I still use C++. Same can be said for XML. You're making it out to be this huge boat that is complex to use, which it doesn't have to be. Also with TinyXML things are even easier as Steve++ pointed out. Quote: Now that is simple. Add some XML tags and it's not simple any more. Overkill for this purpose, as I said. Yeah the text file may look simple, but obviously he's having trouble parsing it so it's not simple to him is it? Anyway, like said above he is using this format for this program, and will probably use another format for another and be on here again in 2 months asking another parsing question. If you are doing this for learning purposes then by all means create your own parser, but if, not why reinvent the wheel. Adding < and > is going to just complicate things? Don't think so. Just my view on it. XML is a great thing that seems to be excepted by people on multiple platforms, so I think we should exploit it while it's there. It will only make it more standard to use than it already is. Which is a good thing in my mind. Also I think is the most rude smiley there is. ======================================================== |
Jonatan Hedborg
Member #4,886
July 2004
|
You COULD use XML, but honestly i think it would be easier to use sscanf. Edited away brainfart. i think.
|
vpenquerch
Member #233
April 2000
|
How funny this is. That reminds me of the Windows philosophy: I can't quite recall what it was, that was some API to Anyway, if you want to use XML, then go for it, but please Use Lua instead! Er, sorry, you can ignore the previous line |
Mika Halttunen
Member #760
November 2000
|
Quote: Anyway, if you want to use XML, then go for it, but please don't try to force it down other people's throats. While the base may be simple to use and easily extensible, I cringe at a simple configuration file being spammed by all these tags. Utterly unreadable. Now which one of the following is more readable? This: [graphics] resolution = 640 x 480 bpp = 32 fullscreen = true [sound] sound_volume = 100 music_volume = 50 sound_enabled = true music_enabled = true ..or maybe this?: <?xml . . .> <options> <graphics> <resolution> <value>640 x 480</value> </resolution> <bpp> <value>32</value> </bpp> <fullscreen> <value>true</value> </fullscreen> </graphics> <sound> <sound_volume> <value>100</value> </sound_volume> <music_volume> <value>50</value> </music_volume> <sound_enabled> <value>true</value> </sound_enabled> <music_enabled> <value>true</value> </music_enabled> </sound> </options> |
Indeterminatus
Member #737
November 2000
|
To defend the readability of a possible XML document a little bit, the XML output can just as well be like this: <?xml . . .> <options> <graphics> <resolution value="640 x 480" /> <bpp value="32" /> <fullscreen value="true" /> </graphics> </options> Or even: <?xml . . .> <options> <graphics resolution="640 x 480" bpp="32" fullscreen="true" /> </options> If that's useful is another question of course. _______________________________ |
Thomas Fjellstrom
Member #476
June 2000
|
Quote: If that's useful is another question of course. Uh, if thats readable is another question of course. -- |
|
1
2
|