I/O HELP

Discussion about everything. New games, 3d math, development tips...
Post Reply
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

I/O HELP

Post by omar shaaban »

well the tutorials on the web on this section is too bad what i understand was
first we include the headers

Code: Select all

#include <fstream>
then if we we want to create a new file:

Code: Select all

std::ofstream level;
level.open("newfile.txt");
but if we wanted to open a file

Code: Select all

std::ofstream level("newfile.txt");
and i knew how to add texts and save it by close()
but i have a problem here:

Code: Select all

std::ofstream level("newfile.txt");
int data;
data=level //here is the error
the error is because the 2 are different one is int and the other is ofstream so how can i make data=level(text it have)!!!!?
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

well this should probably be in beginners help.

even though fstream can be used this is actually an irrlicht question.

Code: Select all

stringw filename;

filesystem->createandopenfile(filename);
blah blah... read the api man.

it has what you need.
Dances
Posts: 454
Joined: Sat Jul 02, 2005 1:45 am
Location: Canada
Contact:

Re: I/O HELP

Post by Dances »

omar shaaban wrote:well the tutorials on the web on this section is too bad what i understand was
first we include the headers

Code: Select all

#include <fstream>
then if we we want to create a new file:

Code: Select all

std::ofstream level;
level.open("newfile.txt");
but if we wanted to open a file

Code: Select all

std::ofstream level("newfile.txt");
and i knew how to add texts and save it by close()
but i have a problem here:

Code: Select all

std::ofstream level("newfile.txt");
int data;
data=level //here is the error
the error is because the 2 are different one is int and the other is ofstream so how can i make data=level(text it have)!!!!?
I don't even understand what you're trying to accomplish with the line data=level... are you trying to get input from the file?

If you're trying to get input you'd use ifstream
for output you'd use ofstream

then you use level as if it were cin or cout.

So if you're trying to get input from the file then you'd go
level << data;

if you're trying to output data, then
level >> data;

BTW theres a second parameter you can set when creating a fsteam, it uses members of the ios namespace, it has things that will create a new file if one doesn't exist, append information, not overwrite, and a few other things.
zeno60
Posts: 342
Joined: Sun May 21, 2006 2:48 am
Location: NC, USA
Contact:

Post by zeno60 »

To get the input would it not be:

level >> data;

And to write to the file:

level << data;

I have always prefered to use the put-buffer and get-buffer operations myself.

Code: Select all

    std::ifstream from("test.txt");
    std::ofstream to("write.txt");
    
    char ch;
    while(from.get(ch)) to.put (ch);
Dances
Posts: 454
Joined: Sat Jul 02, 2005 1:45 am
Location: Canada
Contact:

Post by Dances »

zeno60 wrote:To get the input would it not be:

level >> data;

And to write to the file:

level << data;
Heh, yes, you are right, I wrote operands backwards, thanks.
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

thank you guys u made it clear!! :)
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

will it doesnt work i have an array in data.mtd(just a text file) and then i try to input it

Code: Select all

std::ifstream level("data.mtd");
int map;
level<<data;
myarray[30][30]= data;
but the level value is 000000 and it is supposed to be the array!!!
zeno60
Posts: 342
Joined: Sun May 21, 2006 2:48 am
Location: NC, USA
Contact:

Post by zeno60 »

omar shaaban wrote:will it doesnt work i have an array in data.mtd(just a text file) and then i try to input it

Code: Select all

std::ifstream level("data.mtd");
int map;
level<<data;
myarray[30][30]= data;
but the level value is 000000 and it is supposed to be the array!!!

Code: Select all

level<<data;
Here you are attempting to input 'data' into the file. Think of the arrows:

to here << from here
from here >> to here
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

yes i made it like that i am sory the code was not corrected anyway it doesnt work why!?
the code is:

Code: Select all

std::ifstream level("data.mtd");
int map;
level>>data;
myarray[30][30]= data; 
and the level value is 0000000 and it supposed to give me the array!!?
Dances
Posts: 454
Joined: Sat Jul 02, 2005 1:45 am
Location: Canada
Contact:

Post by Dances »

Since you had that code wrong it would have kept overwriting your file's data with 0, so make sure you've changed the input file.

otherwise your code is okay. I load a config in my game like this

Code: Select all

options filemanager::loadOptions()
{
        options goptions;
        ifstream ConfigFile("settings.cfg");
        ConfigFile >> goptions.fullscreen >> goptions.bits >> goptions.vsync >> 
           goptions.antialias;
        ConfigFile.close();
        return goptions;
}
You might want to output data and then output your array's value and see if they're matching up... perhaps your variables are bad or something.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

You should use the irrlicht built in functions for portabilitty and ease (Unless they are not portable haha)
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

ok here we go again how can i use irrlicht api to load files or save files i read the api but if anyone could give me a small example!
Post Reply