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.
Reading a text file using Irrlicht. (IReadFile)
-
- Posts: 1638
- Joined: Mon Apr 30, 2007 3:24 am
- Location: Montreal, CANADA
- Contact:
-
- Posts: 1638
- Joined: Mon Apr 30, 2007 3:24 am
- Location: Montreal, CANADA
- Contact:
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):
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.
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()...);
}
#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.
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;
}