• Advertisement

Archived

This topic is now archived and is closed to further replies.

Parsing text files???

This topic is 6000 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, I''m trying to learn about how to parse text files in C++ basically, if i were to have a .txt file with some variables and values that i would like to be included in the program at run-time...how would i do that...i believe text parsing is what i''m looking for...any ideas? or resources i could have a look at? any help is much appreciated

Share this post


Link to post
Share on other sites
Advertisement
What you''re looking for is a parser. You can find several implementations in almost all pages about programming, see www.codeguru.com and www.codeproject.com.

There is tools for making that kind od analyzers, for example lex and yacc for unix and a lot of similar ones for windows, but that tools rqeuires more knowledgment about state machines and grammars.

I sugest you that look the two links and look for articles about that. I''d said before, there are a lot of them.



"If you''''re gonna die, die with your boots on"

Share this post


Link to post
Share on other sites
Do you know what .ini files are?

The normally have their contents stored like so:

[Section]

Value1=30
Value2=My name


To access an ini file, you can use GetPrivateProfileString and GetPrivateProfileInt. For example, if the above values were stored in a file called "settings.ini", you can access them like so:

  
int varValue1;
char varValue2[80];

varValue1 = GetPrivateProfileInt("Section", "Value1", 300, "settings.ini");

GetPrivateProfileString("Section", "Value2", "", varValue2, sizeof(varValue2), "settings.ini");


Hope that helps a bit.



"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick

"It is far easier for a camel to pass through the eye of a needle if it first passes through a blender" -- Damocles

Share this post


Link to post
Share on other sites
if you wanna use basic standard c functions you could use:

int sscanf(const char *string, const char *format, ...); and
char *fgets(char *buffer, int maxlength, FILE *file);

here''s a small example:

test.c

  
#include <stdio.h>

float a;
int b;
char c;
char buffer[80];
FILE *txt_file;

int main(void)
{
txt_file = fopen("file.txt", "rb");

while(!feof(txt_file))
{
if(fgets(buffer, 80, txt_file) == NULL)
break;
sscanf(buffer, "%f,%i,%s", &a, &b, &c);

printf("%f, %i, %c\n", a, b, c);
}

return 0;
}


and an example txt file:

file.txt

0.5, 5, c
2.3, 256, m



Hope this helps, and I hope this comes out looking ok since it''s the first time I used those forum tags.

geekster

Share this post


Link to post
Share on other sites
Much thanks for the posts,

basically, i''m trying to do something very similar to Merrick''s response, except i''m guessing this only works with predetermined variables, so if i were to want to add things to my .ini files, i would need to look into link lists and parsers etc, right?

Share this post


Link to post
Share on other sites
What's this obsession with linked lists & parsers

To write a value to your file, you use

WritePrivateProfileString()

    
WritePrivateProfileString ("Section", "Value110", "String to Add", "Settings.ini");

WritePrivateProfiltString("NewSection","AnotherInt", "314", "Settings.ini");


There's also a couple of functions to make your life easier called GetPrivateProfileStruct & WritePrivateProfileStruct, which, as you might have guessed, save out your entire struct in a nice way.



"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick

"It is far easier for a camel to pass through the eye of a needle if it first passes through a blender" -- Damocles


Edited by - Merrick on September 20, 2001 8:09:49 PM

Share this post


Link to post
Share on other sites
quote:

varValue1 = GetPrivateProfileInt("Section", "Value1", 300, "settings.ini");



That's really cool, Merrick. But in GetPrivateProfileInt() what does the 300 do?




A CRPG in development...

Need help? Well, go FAQ yourself.


Edited by - Nazrix on September 21, 2001 7:06:56 PM

Share this post


Link to post
Share on other sites
UINT GetPrivateProfileInt(
LPCTSTR lpAppName, // address of section name
LPCTSTR lpKeyName, // address of key name
INT nDefault, // return value if key name is not found
LPCTSTR lpFileName // address of initialization filename
);


*cough*MSDN*cough*

-----------------------------
-cow_in_the_well

!!!! PeH !!!!

Share this post


Link to post
Share on other sites
quote:
Original post by cow_in_the_well


*cough*MSDN*cough*



Yeah, yeah I know...I tried looking it up online but the one article that talked about it said page not found

.trout MSDN
.cool cow


A CRPG in development...

Need help? Well, go FAQ yourself.


Edited by - Nazrix on September 21, 2001 7:27:53 PM

Share this post


Link to post
Share on other sites
One more question about all this...but again, thanks for the info


using the technique from Merrick''s code on a struct,

how would the struct physically look in the .ini file, and how would it look in code where i want to load it in???

Share this post


Link to post
Share on other sites
Whoops, knew I''d forgotten something. The "" in GetPrivateProfileString is the default string, and the 300 in GetPrivateProfileInt is the default value.

Gee, I forget one thing, and you jump down my throat. Sheesh



"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick

Share this post


Link to post
Share on other sites
quote:
Original post by Merrick
Gee, I forget one thing, and you jump down my throat. Sheesh



"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick


...and don''t let it happen again

Share this post


Link to post
Share on other sites

  • Advertisement