Reading config files -1 reply

Please wait...

Guest

I didn't make it!

0 XP

 
#1 11 years ago

I have this, into "user.cfg" file:

t 100000 0000000 000000100 00001 loloI managed to code a function (inspired on DsOnline source) that reads the file and stores every substring into a char array.

Well, this is the code i have, simplified:

char ParseBuffer[1024]=""; char pass[MAX_STRING_CHARS]=""; char vida[MAX_STRING_CHARS]; char ataques[MAX_STRING_CHARS]=""; char experiencia[MAX_STRING_CHARS]=""; char armas[MAX_STRING_CHARS]=""; char name[MAX_NAME_LENGTH]=""; int pi=0; int espacio=0; fileHandle_t f;

...

len = trap_FS_FOpenFile( va( "user.cfg"), &f, FS_READ);

trap_FS_Read(ParseBuffer, len, f); for ( i = 0; i < strlen (ParseBuffer); i++ ){ if ( ParseBuffer[i] == ' ' ) { pi++; if ( pi == 1 ){ memcpy (pass, ParseBuffer, i); }else if ( pi == 2 ){ memcpy (vida, ParseBuffer+o+1, i-o-1); }else if ( pi == 3 ){ memcpy (ataques, ParseBuffer+o+1, i-o-1); }else if ( pi == 4 ){ memcpy (experiencia, ParseBuffer+o+1, i-o-1); }else if ( pi == 5 ){ memcpy (armas, ParseBuffer+o+1, i-o-1); o = i; } } } memcpy (name, ParseBuffer+o+1, strlen(ParseBuffer)-1);It seems to work, but if i print all separately... :

pass t vida ataques 100000 0000000 experiencia 100000 0000000 000000100 armas 100000 0000000 000000100 00001 name lolo :0wned:

But obviously there's a problem with addresses...

Any kind of suggestion?? thx :)




Deathspike

Freelance Programmer

50 XP

2nd May 2004

0 Uploads

129 Posts

0 Threads

#2 11 years ago

This approach should work quite fine, and i can see no real reason why this isnt working like this. Well, i think its better to use a slightly different approach anyway, first you'd want to clear a string and copy the value you want to that string. Take a look at this bit:

char string[4096] = ""; char store[MAX_STRING_CHARS] = ""; int i = 0, o = 0, number = 0;

for ( i = 0; i <= strlen (string); i++ ) { if ( string[i] == ' ' ) { // Every ' ' is concidered a new string.. number++;

// Clean the storage string! memset(store, 0, sizeof(store));

// Set the storage for those that are going to need it..! memcpy (store, string+o+1, i-o-1);

switch (number) { // Do your thingies here.. }

// We have to set the 'o' at the end because this will be the start for the next o = i; } }




Guest

I didn't make it!

0 XP

 
#3 11 years ago

Deathspike;3538331// We have to set the 'o' at the end because this will be the start for the next[/quote]

:kerian:

Well, that's the problem.

[quote=visiat;3537221] }else if ( pi == 5 ){ memcpy (armas, ParseBuffer+o+1, i-o-1); o = i; } } }

o=i is done if "pi==5", so its a clause problem.

}else if ( pi == 5 ){ memcpy (armas, ParseBuffer+o+1, i-o-1); } o=i; } } Now works perfectly :D

Sorry... and thanks :)




Deathspike

Freelance Programmer

50 XP

2nd May 2004

0 Uploads

129 Posts

0 Threads

#4 11 years ago

Ahhh i see, didnt notice myself either xD The good is that it works, have fun :)