• Welcome to Valhalla Legends Archive.
 

problem getting from ini/cfg via c++ dll 8\

Started by l)ragon, June 15, 2003, 04:11 PM

Previous topic - Next topic

l)ragon


BOOL __stdcall GetDxData(LPSTR data){
   FILE *stream = fopen("Dx.cfg", "r");
   char buf[2048] = "";
   if(stream){
      while(fgets(buf, sizeof(buf), stream)){
         strtok(buf, "\r\n");
         if(!strnicmp(buf, "Dx9Data1=", 9))
            sprintf(data, "%s", buf + 9);
      }
   }
   fclose(stream);
return TRUE;
}


Yes i got motivated enough to finish this myself go nuts works fine <<-- Edit
*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*ˆ¨¯¯¨ˆ*^~·.,l)ragon,.-·~^*ˆ¨¯¯¨ˆ*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*

Eibro

char *  strtok ( const char * string, const char * delimiters );
strtok returns a pointer the first occourance of any characters included in delimiters, you're completly discarding the return value of it. Also, if buf contains more than one newline, that call will only return the first occourance. You need to call strtok multiple times to find multiple newlines in buf. Look here: http://www.cplusplus.com/ref/cstring/strtok.html for an example of using strtok.

It seems to me you should be using something like fgetln instead. But then again, I usually use C++ stream objects for working with files. Also, I'd advise against using the name of a C++ class as a variable name,  (fstream) simply including fstream will give you errors.
Eibro of Yeti Lovers.

l)ragon

strtok wasent the problem, It was just the way i was going about doing the function anyway that Edit is the way i ment for it to work.
*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*ˆ¨¯¯¨ˆ*^~·.,l)ragon,.-·~^*ˆ¨¯¯¨ˆ*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*

Eibro

That call to strtok isn't doing anything. You're simply reading in 2048 characters and checking the first 9 chararacters against "Dx9Data1=". So if "Dx9Data1=" doesn't reside at the head of the chunk, you won't find it.
Eibro of Yeti Lovers.

Adron

This looks like how I often use strtok when I'm a bit lazy. It will replace any trailing \r or \n with \0 - i.e. truncate the string. Also, the fgets loop will read in one line at a time, so you'll never have anything after the possible \r\n.

Eibro

Bah, don't mind me. I didn't notice fgets would stop at a newline.
Eibro of Yeti Lovers.