Game Development Community

Convert readLine()

by Dane McGreevy · in Technical Issues · 02/11/2007 (7:48 pm) · 3 replies

I'm new to c++ and still trying to fully understand pointers. What I'm trying to do is read in point data from a text file, store the point as a variable... multiply the variable by 2 and print it to the console. I'm getting a:

"error C2664: 'atof' : cannot convert parameter 1 from 'const U8 *' to 'const char *'"

Here is my code:

FileObject file;
  if(file.openForRead("D:/Torque SDK/example/polygon1.txt"))
  {
	const int *numberofrecords;
	numberofrecords = (const int *) file.readLine();
	Con::printf("fxFoliageReplicator - Number of records = %s", numberofrecords);

	float fileline;
    while(!file.isEOF())
    { 
	  fileline = atof(file.readLine());
	  float filetest = fileline * 2.0;
	  Con::printf("fxFoliageReplicator - ReadLine = %s", fileline);
	  Con::printf("fxFoliageReplicator - ReadLine = %s", filetest);
    }
  }
  file.close();

Any ideas on doing U8 -> float?

Thanks in advance.

#1
02/12/2007 (12:21 pm)
DSscanf is your friend.

It's prototyped in platform.h:
extern int    dSscanf(const char *buffer, const char *format, ...);

Gary (-;
#2
02/12/2007 (12:39 pm)
Example from what I assume is your format, that probably won't work because I'm making this up as I go along:

FileObject file;
const char *fileLine;
const char *fileName = ""D:/Torque SDK/example/polygon1.txt";

if(file.openForRead(fileName))
{
  int numberofrecords;
  fileLine = file.readLine();
  if(1 != dSscanf(fileLine, "%i", &numberofrecords)) {
    Con::warnf("fxFoliageReplicator - couldn't read number of records");
  } else {
    Con::printf("fxFoliageReplicator - Number of records = %s", numberofrecords);
  }

  float record;
  int currrecords = 0;
  int goodrecords = 0;

  while(!file.isEOF())
  { 
    fileLine = file.readLine();
    currrecords++;
    if(1 != dSscanf(fileLine, "%f", &record)) {
      Con::warnf("fxFoliageReplicator - line %i didn't parse as a float", currrecords);
    } else {
      Con::printf("fxFoliageReplicator - record: %f", record);
      goodrecords++;
    }
  }
  Con::printf("fxFoliageReplicator - Looked for %i records, found %i total and %i good ones", 
          numberofrecords, currrecords, goodrecords);
} else {
  Con::warnf("fxFoliageReplicator - couldn't open file %s", fileName);
}
file.close();

Gary (-;

EDIT: PS Yes, I like Con::warnf because the assert code on linux is pathetic :-P
#3
02/12/2007 (8:06 pm)
Great. Thank you very much Gary for your help. Aside from a few quite mods (adding (const char*) before file.readLine()) it appears to work. Thanks again.