Read File Problems
-
- Posts: 40
- Joined: Wed Oct 06, 2004 7:48 pm
Read File Problems
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
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
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):
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.
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);
}
}
Not checked the code for typos etc, but it should be pretty free of errors.
-
- Posts: 40
- Joined: Wed Oct 06, 2004 7:48 pm
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.
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.
-
- Posts: 40
- Joined: Wed Oct 06, 2004 7:48 pm
-
- Posts: 40
- Joined: Wed Oct 06, 2004 7:48 pm
-
- Posts: 40
- Joined: Wed Oct 06, 2004 7:48 pm
-
- Posts: 40
- Joined: Wed Oct 06, 2004 7:48 pm
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.
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.
Reading File using STL
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
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