Reading a text file using Irrlicht. (IReadFile)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Reading a text file using Irrlicht. (IReadFile)

Post 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.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post 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.
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Post by christianclavet »

Thank Hybrid. I'll redo an attempt in the weekend.
AlkaSoft
Posts: 7
Joined: Sun Dec 20, 2009 11:13 pm

Post 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.
Last edited by AlkaSoft on Wed Jan 13, 2010 12:34 am, edited 1 time in total.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, OT was about Irrlicht file functions, where you can also store files in zip archives, encrypt, etc.
AlkaSoft
Posts: 7
Joined: Sun Dec 20, 2009 11:13 pm

Post by AlkaSoft »

Wow i didnt knew that this thing existed lol. It looks like much more complete and easy to use.
monchito
Posts: 59
Joined: Thu Mar 08, 2007 9:38 pm

Post 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;
}
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post 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.
Post Reply