Game Development Community

Reading a line from a file.

by Sam Guffey · in Torque Game Engine · 03/14/2004 (5:10 pm) · 12 replies

Is there an expample of how I can use "openFileForRead" and read a specific value from a file?

I am using an external program called Curved to setup a vehicle torque curve. I am needing to be able to read the settings in that file.

Heres a small example of what the file holds.
curve
{
  xname=RPM
  yname=Torque
  xsteps=10
  ysteps=10
  xmin=0
  ymin=0
  xmax=10000
  ymax=1000
  points=8
  point0
  {
    x=466.000000
    y=65.000000
  }

I need to read "xmax, ymax and points". And I'll need to read point0.x and point0.y

I'm a novice at this so please use elementary talk.

Thanks for any help.

#1
03/14/2004 (5:23 pm)
If you can guarantee that each entry is on a known line you could just skip lines until you get to one you care about, then snarf the value that you want out.

Or you can use lex/yacc to do it, which would be preferable but more difficult.
#2
03/14/2004 (6:55 pm)
Sam: I'm not sure if I'm reading you right, but it sounds like you need to read it in Torque after the data has already been written by Curves. If so... download the TorqueIDE stuff. Don't bother installing it (well, you can if you want to :-) and just look through the scripts - there's a couple o' ways to pull this off with Torque Script that were used in there, ranging from reading the whole file in and looking for carrage returns to using getRecord. (TorqueIDE has multiple contributors, so, sometimes the same thing was done differently :-)
#3
03/14/2004 (7:54 pm)
This works but man oh man, why isnt there an easier way.

Stream *stream = ResourceManager->openStream(file);
   Vector<CurveInfo>::iterator info;
   char buffer[256];
   for (;;)
   {
      if(stream->getStatus() == Stream::EOS)
         break;

      stream->readLine((U8*)buffer, 256);
      Con::errorf("%s", buffer);
   }
This lists the entire file in the console perfectly.

I want to use the CurveInfo to store the info into a struct but its such a pain. Why isnt there a readLine(10, file); where 10 = the accual line number lol.
#4
03/14/2004 (10:10 pm)
How do I read a string after a certain character in the buffer?
I can find "xmax=10000" so I need to read the buffer after the '='. Is there an engine function for this?
#5
03/15/2004 (12:18 am)
You need to learn how to tokenise Sam! strTok or similar :) tokenise the input and look for the point.x= token and read the token after it. etc..

Have a look how the missioninfo is taken out of the .mis file. That should give you some pointers to loading and parsing files etc.
#6
03/15/2004 (12:20 am)
Oh and another suggestion.. if there is a stable structure in that text, i.e. if that text has always the same format. you could cheat and setup a editor macro (devstudio) that simple replaces strings and pokes it into a torquescript format..

Or write a perl/othermacrolanguage thing to do the same?
#7
03/15/2004 (2:28 am)
Your right Phil, I need to learn.. The last fews days I've dug into the engine more than I have in nearly 2 years. I accually know what dAtoi/f/b is now lol. If I get this right I will have an awesome true to physics vehicle. (Engine that is) RPM, Torque all based on rear end ratio, transmission ratio and wheel diameters.
#8
03/15/2004 (2:45 am)
If your looking for a decent tokenizer, try ripping out tokenizer.cc from map2dif :)
#9
03/15/2004 (3:22 am)
Yeah but I wouldnt know how to use it James. Hahahaaaawwwww! :(
#10
03/16/2004 (4:34 am)
Would anyone be willing to help me with this? I really dont know much about it.
#11
03/16/2004 (5:34 am)
Sorry to hi-jack this thread, is it possible to fread/fwrite in binary from script?
#12
03/16/2004 (5:52 am)
Sam: you could just read the file line per line, searching for the "=" character...
with something like
if(dStrstr(currLine, "=") != -1)
and then, if found, strip the string on that line in two parts, the part before the "=" and the part after and save those in some array... there should be functions to get substrings in Torque although I cant remember the names right now...
something like getSubStr() in script...
you also might want to trim the lines before further investigating them ...
EDIT:
Oh, there is a dStrtok() function! :)
char * token = dStrtok(myBuffer, "=");
   while(token != NULL)
   {
      if (dStricmp(token, "foo") != 0) 
      {
         Con::print("Found foo!");
      }
      token = dStrtok(NULL, "=");
   }
Not really sure if this will work (didnt test it and have never used the function, but look into the engine for usage examples...