Text Parser Advanced

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
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Text Parser Advanced

Post by Midnight »

after-life wrote:i think its not very smart to use the xml reader/writer for a couple of reasons:

- the file you read has to be xml format
- writing the files seems to be a bitch :p
- its so easy to write your own reader/writer (scroll a bit down to see how)
- its hard to read from the file, every time you read from it you have to know exactly where the code you want is.
-...


Writing your own reader/writer:

I'm not going to explain everything, just the basics, if you want a more advanced explanation, googled it up :D.

The best way to show it is with an example so here we go:

Code: Select all

#include <iostream> //we need this for cout and cerr
#include <string> //we need this to use strings
#include <fstream> //we need this to read from the file

using namespace std;

int main()
{
    string word;  
  //an empty string to put data from the file in

    char filename[] = "config.cfg"; 
  //a string (in the form of a char with an array) with the filename

    string object("bitrate"); 
  //this is the object we are going to search for

    int bitrate; 
/*Here we will store the attribute of the bitrate. Because we know it will be 16, 32 or maybe 64, we use an int. If you want to have a bool, you should use a string and make a little if statement*/
    
    ifstream inputfile(filename); 
  //we declare inputfile so we can then read from it
    
    if(!inputfile.is_open()) //we check if inputfile is open
    {
        cerr<<"ERROR; Couldn't open "<< filename <<endl;  
  //if its not open we show an error

        system("pause");
  //pause it so people can read it

        exit(1); 
  //and exit
    }
    
/*now we make a while loop so that it inputs a word into our string: word. Every time it does the while loop, it enters the next word into our string starting with the first word if the word is the object we are looking for, we exit the file.*/
    
    while(word != object) 
    {
        inputfile >> word; 
    }
    
    inputfile >> word; 
  //now we just need to load in the attribute, which is the next word

    cout << word <<endl; 
  //for the sake of this example, we output it to the screen
    
    system("pause");
    return 0;
}

Now, here is my config.cfg file :

Code: Select all

driver opengl
resX 1024
resY 768
bitrate 32
fullscreen true
as you can see, easy to write and easy to read.
all you have to do is make it into a class and enjoy :D

btw, if you want to write from a file, you can use ofstream instead of ifstream and then do outputfile<<word;
so you just add word to the outputfile.
Good luck and have fun with it :D

You will never have does XML bugs, ever!!!
I'm attempting to get this working because it will work where XML won't for me at least not as easily.

the problem is Irrlicht has it's own class (suprise suprise) for strings.

instead of string Irrlicht over rides that command and uses stringw.

however in this example

Code: Select all

     inputfile >> word; 
  //now we just need to load in the attribute, which is the next word

    cout << word <<endl; 
  //for the sake of this example, we output it to the screen
this command "<<" is a console command.. I need the equivalent in ifstream or whatever.. I used peek() to get the next word I believe..

anyways I'm really confused can anyone help sort this out or show me there own text parser??
T101
Posts: 44
Joined: Thu Jul 29, 2004 4:41 pm

Post by T101 »

"Console command"?

cout, cin and cerr are just stream objects.
File streams inherit from the same class as the "console" streams.

The "<<" operator is supported by all stream objects - such as cout and cerr.

You should be able to just use that same operator to write to a file stream.

The ">>" operator reads from a stream. Again, keyboard (cin) or file does not matter (although obviously you'll have to set up the stream first).
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

ok thats what I thought since I use those commands in the save code.

however it was giving me errors I'll have to try again since I removed it from my app and get back to ya.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

binary '>>' : no operator defined which takes a right-hand operand of type 'class irr::core::string<unsigned short>' (or there is no acceptable conversion)


thats the error I get from this line

inputfile >> word;



thats using stringw in place of string since otherwise I get this error


'string' : ambiguous symbol



can anyone clue me in??
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

try using std::string instead?
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

ok I think I got this figuared out sorta.

if I use std::string I avoid the ambiguous symbol error.

however it appears to be returning a pointer to the string and not the actual variable I'll have to find a way to convert it I guess.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

that was a quick response bitplane thanks.

however I solved "that" problem on my own.

I'm having a bit of trouble understand the results though..

if I do this example using only a console app I get the exact contents of the string but in my application I'm having it (as a test) print out the contents of "word" into the window caption and instead of 16 or 32 I'm getting what appears to be a pointer specificly this -858993460 or something like that which tells me it's a pointer to the string and not the contents of the string itself.

can anyone help me get past this please??
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

oop I think i got it... tried to advance the error code to print out to console.

seems to be returning the same value even when it doesn't read the file so I guess that would mean I'm declaring it incorrectly or something.
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

i'm assuming you're trying to output a wchar_t string? stream objects do not have a proper overload for wchar_t, at least not in Visual Studio. You'll need to convert it to a char* string.

If you need to convert from a whcar_t* string to a char* string, look into the function - wcstombs - http://msdn.microsoft.com/library/defau ... stombs.asp
Image
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

Code: Select all

#include "../Guice/Guice.h"
#include <string>

void Guice::Read()
{
	//a string (in the form of a char with an array) with the filename 
    char filename[] = "Data/config.cfg";
    
	std::string word;

	//this is the object we are going to search for 
	std::string object("bitrate");

    
	//we declare inputfile so we can then read from it 
    ifstream inputfile(filename);
    
    if(!inputfile.is_open()) //we check if inputfile is open 
    { 
		Console->addItem(L"ERROR: file was not opened");
		setscroll();
		return;
	}

	
	while(word != object) 
    { 
        inputfile >> word; 
    } 
    
	//now we just need to load in the attribute, which is the next word 
   inputfile >> bitrate;

}
This is what finally worked for me I eliminated the string and replaced with an integer (that already existed for other purposes)

anyways I can take it from here thanks for all the help guys.
Post Reply