Read File Problems

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
Fliege_reloaded
Posts: 40
Joined: Wed Oct 06, 2004 7:48 pm

Read File Problems

Post by Fliege_reloaded »

Hi,
I've got a problem:
I want to read lines out of my data, but where I have to safe them? Can anyone give me a short example of opening a file, loading a line of it and check if the end of this file is reached? Please, I need it!
Thanks
Fliege_reloaded
T101
Posts: 44
Joined: Thu Jul 29, 2004 4:41 pm

Post by T101 »

Not talking about Irrlicht functionality here, but here's how you can do it in C (that also works with C++, unless you have really old standard header files):

Code: Select all

#include <stdio.h>
#include <ctype.h>

#define MAXLINESIZE 512

char line[MAXLINESIZE+1];

main()
{
 FILE *fp;
 int c,length;
 char done;    // using char here - but you can use a bool type

 length=0;
 done=0;

 fp=fopen("mytextfile","r");

 if(fp) {
  while(!done) {
    c=fgetc(fp);
    switch(c) {
     case EOF:
      done=1;
      // continue through to the end-of-line code here - so no break
     case '\n':
      line[length]='\0';
      // now do something with the text stored in the line buffer
      printf("line: %s\n", line);
      // you can use sscanf to parse the contents into name, value pairs or whatever you like
      length=0;
      break;
     default:
      if(isprint(c) && length<MAXLINESIZE) {     // suppress control characters and prevent buffer overflow
       line[length++]=c;
      }
      break;
   }
  fclose(fp);
 }
}
There are more elegant ways, but they also tend to be more complex.
Not checked the code for typos etc, but it should be pretty free of errors.
Fliege_reloaded
Posts: 40
Joined: Wed Oct 06, 2004 7:48 pm

Post by Fliege_reloaded »

NO, there is a way to load these things in Irrlicht, so please tell me about. Please give me an example, how to read a line intoa stringc string. Please
Fliege_reloaded
T101
Posts: 44
Joined: Thu Jul 29, 2004 4:41 pm

Post by T101 »

Judging from the API page, Irr only offers raw I/O on simple text files.

You allocate a buffer, then you call read(bufferaddress,buffersize) and it returns the number of bytes read.

I'm actually not sure why this is part of Irr - the same functions can be used on all platforms anyway.

The stdio functions also work everywhere.

But the XML reader/writer might be something for you. There's sample code for that in the API page.
Fliege_reloaded
Posts: 40
Joined: Wed Oct 06, 2004 7:48 pm

Post by Fliege_reloaded »

How I create a buffer? I know, I'm a noob to this language!
Fliege_reloaded
Fliege_reloaded
Posts: 40
Joined: Wed Oct 06, 2004 7:48 pm

Post by Fliege_reloaded »

Can anybody give me an example, how to open a file, read a line out of it and put it into a string, and then it should create a Terrain with this string as a path to the heighmap?
Please help me, if got nothing to work...
Fliege_reloaded
Fliege_reloaded
Posts: 40
Joined: Wed Oct 06, 2004 7:48 pm

Post by Fliege_reloaded »

I know that this is spam, but why nobody helps me?
Fliege_reloaded
Posts: 40
Joined: Wed Oct 06, 2004 7:48 pm

Post by Fliege_reloaded »

I know that this is spam, but why nobody helps me?
bal
Posts: 829
Joined: Fri Jun 18, 2004 5:19 pm
Location: Geluwe, Belgium

Post by bal »

Search this forum. Tyn and also me have already provided some example code about the XML reader.
General Tools List
General FAQ
System: AMD Barton 2600+, 512MB, 9600XT 256MB, WinXP + FC3
ZeroMem

Post by ZeroMem »

I'm not sure, but I suppose, that you're need to do it like in programs which using STL(Standart Template Library). I think "stringc" type is defined like "const string". To cut a long story short, you're need to change a bit T101 example:

after #include directives add these lines:

#include <string>
using namespace std;

And in the end, of the T101 code (after the "fclose(fp);") add this:

stringc str = line;

After that, str shoud contains the line.
Guest

Reading File using STL

Post by Guest »

Hi (I'm too lazy to log in. It will be posted in guest mode)

Here's my code to load data from a file using STL. Since I use std::vector, the code may be a bit slow if you have a lot of lines to read from the file (The vector object resizes itself when the number of positions is out and this takes time). Set the initial size of the vector with the number of lines in the file to optimize this.

I have a class that encapsulates the code bellow but it is too specific for my game, but I think you guys can figure out how to make your own class with the code.


-------------------------------------
// The includes

#include <cmath>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>


// The code to read from a file using STL

// Open the file containing the parameters of the game
ifstream gameFileIni("myfile.ini");

/*
* myfile.ini goes like this
*
* Fullscreen=false
* StencilBuffer=false
* ScreenWidth=800
*
* The variable "position" bellow gives the first character after the "=" sign
*/


// Iterators to read from the file
istream_iterator<string> inIter(gameFileIni);
istream_iterator<string> endIter;

vector<string> initialInput;

initialInput.reserve(80);


// Character showing the end of the line in the file.
string endLine = "\n";


// Store the parameters from the file in the Vector initialInput
copy(inIter, endIter, back_inserter(initialInput));


// Initialize the index to run through the vector. Index gives the
// line number you are reading at a moment.
int index = 0;

// aux variables
string aux;
int position;


// code to read a boolean.

position = initialInput[index].find(endLine, 0); // the no. of chars of this line
aux = initialInput[index].substr(11, position);

// 11 is the number of characters of before the value I want to read
// 1 2 3 4 5 6 7 8 9 10 11
// F u l l S c r e e n =false

bool tempBool;
if(aux.compare("true") == 0) {
tempBool = true;
}
else {
tempBool = false;
}

bool fullScreen = temp;
index++;

// code to read an integer
position = initialInput[index].find(endLine, 0);
aux = initialInputc[index].substr(13, position);
int tempInt = ::atoi(aux.c_str());
int screenHeight = tempInt;
index++;

Hope that helps
Post Reply