Loading data from a text file.

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
JediTrooper87
Posts: 8
Joined: Sat Jun 16, 2007 6:13 am
Location: Kansas

Loading data from a text file.

Post by JediTrooper87 »

Hi everyone, my first post, but I've been lurking for awhile. I've been using Irrlicht for a few weeks now and have gone through all the tutorials at least once. I'm still fairly new to C++ but I'm picking it up fairly quickly and I have a lot of experience with a few different scripting languages.

I've been experimenting with Irrlicht and I've come to something that I can't figure out how to do. Say I have a text file(.txt) that is split into multiple sections and I have each section labeled with a header and the next line after the header holds a number that tells how many lines are in the section. Much like this:
---
HEADER
numlines 3

0.{data}
1.{data}
2.{data}

SECONDHEADER
numlines 1

0.{data}
---
Now, I know how to read the right number of lines after the 'numlines' string[very basicly: for( i = 0; i < numlines-1; ++i ){}], but what I can't find is how to search through the file for the specific string 'HEADER' using Irrlicht's string system rather than Windows' system. Since the text file will be only a template and not of a static size, I can't just tell the code to look at a specific line number. I'm guessing it has to do with find() in irr::core::string, but that says it's to find a string in another string, so would I have to load the entire file as a string then use something like file.find("HEADER");?
Am I on the right track or is there an easier way to achieve this?

Thanks for any help in advance.
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Check the Windows like INI loader code snippet in the code snippet section, should start you up ;)
JediTrooper87
Posts: 8
Joined: Sat Jun 16, 2007 6:13 am
Location: Kansas

Post by JediTrooper87 »

Ahh, thanks. Guess I missed that while searching the forums. I'll post again if I can't get it working. :D
roxaz
Posts: 575
Joined: Tue Jan 23, 2007 8:35 pm
Location: LT

Post by roxaz »

i can provide you with a concept how to parse text files, i have done it several times, several parsers to download data from websites ;)

so the way i would do it is that i would split the file in to an array. each array element holds one line. then i would loop the array and look lets say for header. When header is detected - we do other specific operations to get required data from next array elements (line).

I hope this will help. Too bad i cant provide you with code or something more specific because i have done such thing with php only, no c++ experience in text parsing :) also if you dont mind using framework, there are a lot of useful functions for string parsing ;)
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

how to search through the file for the specific string 'HEADER' using Irrlicht's string
I'm guessing it has to do with find() in irr::core::string, but that says it's to find a string in another string, so would I have to load the entire file as a string then use something like file.find("HEADER");?
Exactly. You firsth load data from file in to string then perform operations on it.
Post Reply