reading from a file
I am making a text rpg, and want to read from a textile to display ascii art, instead of cout-ing every line, but I don''t know how to make the file comeout exactly how it is in the textfile, everything only comes out in onecloumn, like a list, and I have no Idea how to display it exactly how it is in the text file.
can someone help me out?
"Only to be a humble programmer is a goal I must achieve..."
void ShowOgre(){ ifstream infile; char pic; infile.open("Ogre.txt",ios::in); while (!(infile.eof())) { infile >> pic; cout << pic << endl; } infile.close();}
I know I should have an array but i don''t know, how big or how to initialize it to do what I want.
"Only to be a humble programmer is a goal I must achieve..."
''cout << pic << endl;''
You are outputting a newline after each character. Just change this line to ''cout << pic;''.
You are outputting a newline after each character. Just change this line to ''cout << pic;''.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Ah... I'm assuming the file reading stuff works, but you need something more like this:
I'm not sure if that's right, but it's something like that. Basically, since the image has both width and height, you need to loop through them and output that way. To go to the next line, you will check for when you reach the end of the line on your picture (in this case, I called it MAX_WIDTH) and cout<
[edited by - Peon on May 16, 2002 9:32:56 PM]
[edited by - Peon on May 16, 2002 9:33:48 PM]
for (int i=0; i <= MAX_HEIGHT; i++){ for (int j=0; j <= MAX_WIDTH; j++) { infile >> pic; cout<<pic; if (j == MAX_WIDTH) { cout<<endl; } }//endfor}//endfor
I'm not sure if that's right, but it's something like that. Basically, since the image has both width and height, you need to loop through them and output that way. To go to the next line, you will check for when you reach the end of the line on your picture (in this case, I called it MAX_WIDTH) and cout<
[edited by - Peon on May 16, 2002 9:32:56 PM]
[edited by - Peon on May 16, 2002 9:33:48 PM]
Peon
taking off the endl does nothing,it just displays evrything horizontally I want it to display exactly how it is in the txt file.
"Only to be a humble programmer is a goal I must achieve..."
MAX_WIDTH and HEIGHT are cin.getline?
or
would I make an array
pic[MAXWIDTH][MAXHEIGHT]
something like that?
or
would I make an array
pic[MAXWIDTH][MAXHEIGHT]
something like that?
"Only to be a humble programmer is a goal I must achieve..."
Maybe you can try:
#include
void ShowOgre()
{
ifstream infile("Ogre.txt";
string pic;
while (getline(infile,pic)
{
cout << pic << \n;
} infile.close();
}
getline(infile,pic) will read a line from Ogre.txt and assign pic to that value.
The string type is a sort of self-resizing array of char. Hope this helps.
#include
void ShowOgre()
{
ifstream infile("Ogre.txt";
string pic;
while (getline(infile,pic)
{
cout << pic << \n;
} infile.close();
}
getline(infile,pic) will read a line from Ogre.txt and assign pic to that value.
The string type is a sort of self-resizing array of char. Hope this helps.
Ahh, right. The stream insertion and extraction operators don''t do what we want in this case. Change the ''infile >> pic'' line to ''infile.get(pic);''
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
I appreciate the help, I have been trying your suggestions, but I keep getting errors now, when I use get and get line;
Ok I got it to work, thanx to your suggestions, this place is the best,
Thanx alot guys;
if your curios about the ogre here it is, not made by me.
Ogre
__,='`````'=/__
'// (o) \(o) \ `' _,-,
//| ,_) (`\ ,-'`_,- ,-~~~\ `'===' /-, \==```` \__
/ `----' `\ \ \/
,-` , \ ,.-\ / , \,-`\`_,-`\_,..--' ,` ,/, ,>, ) \--````` ( `\`---'` `-,-'`_,< \ \_,.--'`
`. `--. _,-'`_,-` | [`-.___ <`_,-'`------( /
(`` _,-\ \ --`````````|--`
>-`_,-`\,-` , |
<`_,' , /\ /
` \/\,-/ `/ \/`\_/V\_/
( ._. ) ( .__. )
| | | |
\,---_| |_---./
ooOO(_) (_)OOoo
[edited by - gamefr on May 16, 2002 10:33:21 PM]
Ok I got it to work, thanx to your suggestions, this place is the best,
Thanx alot guys;
if your curios about the ogre here it is, not made by me.
Ogre
__,='`````'=/__
'// (o) \(o) \ `' _,-,
//| ,_) (`\ ,-'`_,- ,-~~~\ `'===' /-, \==```` \__
/ `----' `\ \ \/
,-` , \ ,.-\ / , \,-`\`_,-`\_,..--' ,` ,/, ,>, ) \--````` ( `\`---'` `-,-'`_,< \ \_,.--'`
`. `--. _,-'`_,-` | [`-.___ <`_,-'`------( /
(`` _,-\ \ --`````````|--`
>-`_,-`\,-` , |
<`_,' , /\ /
` \/\,-/ `/ \/`\_/V\_/
( ._. ) ( .__. )
| | | |
\,---_| |_---./
ooOO(_) (_)OOoo
[edited by - gamefr on May 16, 2002 10:33:21 PM]
"Only to be a humble programmer is a goal I must achieve..."
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement