Page 1 of 1

Reading a text file using Irrlicht. (IReadFile)

Posted: Sat Jan 09, 2010 9:15 pm
by christianclavet
Is there a tutorial somewhere that I could use to read a text file using IReadFile.

I would like to use this to "parse" a simple text file. Get the arguments from it. It's not a XML file. Checked the source of IRRlicht and I have difficulties to understand how do they do it.

Posted: Sat Jan 09, 2010 10:34 pm
by hybrid
If you have plain text files, with variable length tokens, you have to read the characters one by one. Otherwise you could read more bytes and hence become a little more efficient. But that's it basically.

Posted: Tue Jan 12, 2010 4:52 pm
by christianclavet
Thank Hybrid. I'll redo an attempt in the weekend.

Posted: Tue Jan 12, 2010 6:15 pm
by AlkaSoft
I have made a code that loads the some data from a text file, i won't post the full code right now, but i could tell you how i did it :

1 :Open notepad and create a empty file, name it (I named mine "Data.options", i changed the the type of file, many people dont try to open ".options" file), and write the data you would like to load, each one followed by a space.
Ex : 800 600 32 0
Where 800 and 600 are resolution, 32 are bits and 0 is fullscreen.

2 : To open this file and to directly read the data, initialise 4 variables where you could put these values in, then, use this code (pseudo-code):

Code: Select all

LoadOptions.cpp


FILE* Options = fopen("Data/Data.options", "r+");
if (Options != NULL)
    {ReadValues();}
    else
    {WriteNewValues();}
    VerifyAllValues(); ///Check If the file is incoherent or has been modified, if yes, erase the values and write default ones, then reload data.

...


///Here is a part of the function to recreate a file///
if (Options != NULL)
    {fclose(Options);
    remove("Data/Data.options");}
    Options = fopen("Data/Data.options", "w");
    rewind(Options);
    fputs("640 480 32 0", Options);
...


///Here is the part of code to read a file///
rewind(Options);
fscanf(Options, "%d %d %d %d", &RESX,&RESY,&BITS,&SCREEN);
///This inserts all values (%d) in the corresponding variable.
...


///Here is to return a readed value///
int getResolution_X()
{return RESX;}
...


///get the value and return it to your Device Parameters
In main.cpp
{
...
createDevice(Options->getResolution_X()...);
} 
I think you get it. You simply need to implement this as a class, major functions you need are here. BTW : this functions (fscan, fopen) are C code... You should also include :
#include <stdlib.h>
#include <stdio.h>

Hope this helps you.

Posted: Tue Jan 12, 2010 8:19 pm
by hybrid
Well, OT was about Irrlicht file functions, where you can also store files in zip archives, encrypt, etc.

Posted: Wed Jan 13, 2010 12:36 am
by AlkaSoft
Wow i didnt knew that this thing existed lol. It looks like much more complete and easy to use.

Posted: Wed Jan 13, 2010 12:59 am
by monchito
I did some practice with this

Code: Select all


bool readline( io::IReadFile* f, core::stringc* str )
{
  char  ch;
  *str = ""; 
  while ( 0 != f->read( &ch, 1 ) )
  { 
    if ( ch == '\n')
      return true;
    else
      *str += ch;
  }

  return false; 
}

bool readtoken( io::IReadFile* f, core::stringc* str )
{
  char  ch;
  *str = "";
  while ( 0 != f->read( &ch, 1 ) )
  { 
    if ( ( ch == ' ') || ( ch == '\t') )
      return true;
    else
      *str += ch;
  }

  return false; 
}

int main(int argc, char *argv[])
{ 
  char filename[] = { "e:/test.txt" };
  IrrlichtDevice*  irrDevice = createDevice ( video::EDT_NULL, 
                               core::dimension2d<s32> ( 0, 0 ), 16, false );

  if(!irrDevice)
    return 1;

  io::IFileSystem* filesys = irrDevice->getFileSystem();
  if ( !filesys->existFile ( filename ) )
  {
     cout << "File not found " << filename << endl;
     return 1;
  }

  io::IReadFile* textfile = filesys->createAndOpenFile( filename );
  if ( !textfile )
     return 1;

  core::stringc s;

  while( readtoken( textfile, &s ) )
    cout << s.c_str() << endl;

  textfile->drop();
  irrDevice->drop();
  
  system("PAUSE");
  return EXIT_SUCCESS;
}

Posted: Wed Jan 13, 2010 8:52 am
by hybrid
Yes, that's exactly the way to read the files character by character. I'd suggest to use references instead of pointers for the string passing, but that's just a matter of taste basically.