Reading in from a text flatfile.
I'm writing a program that will allow for scripters to plug in text flatfile "modules" to create an RPG. Basically an RPG engine. The biggest problem I am having at this point in time is reading in from the flatfile to run inside of my engine.
Where I am stuck is moving the file position pointer to the beginning of the line that I need to read data in from. This position will not be static to a file, so I have to be able to dynamically read it.
For example, here's the (broken) code that I have right now:
Code:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
char* GetInfo(int varname);
int main()
{
GetInfo('HP');
cout << "\n\n";
return(0);
}
char* GetInfo(int varname)
{
char stream[256];
fstream file("test.txt", ios::in | ios::out | ios::nocreate | ios::noreplace);
file.ignore(65535, varname);
file.getline(stream, 32,'|');
file.close();
return(stream);
}
stdlib will be for the atol() command so I can convert some strings to ints when I have to.
Here's the flatfile that I am trying to read from:
Code:
MONSTER
Name | Wyvern
HP | 27 | 2 | 5
MP | 0 | 0
AGI | 10 | 1
CHR | 10 | 1
INT | 10 | 1
STA | 10 | 1
STR | 10 | 1
WIS | 10 | 1
XP | 120 | 50
GP | 40 | 10
Head | Helmet | 1
Torso | Belt | 3
Arms | Sleeves | 2
Legs | Breeches | 2
Feet | Boots | 1
What I would like to do is to be able to pass the name of the variable I want (such as "HP") to the GetInfo function, and have it read through the flatfile for that identifier. Then be able to move over past the delimiter and the space (maybe with seekg?) to read the line information.
Any advice?
Thank you in advance. :)
Edit: The code I am posting is from a seperate module than the RPG itself. I'll integrate it into the actual code when it works. ;)