Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » reading text file

This thread is locked; no one can reply to it. rss feed Print
reading text file
jools64
Member #8,718
June 2007

hey,
For the last few days I've been working on my first game which is a simple arcade game and im basically finished but iv just got to add a very simple highscore system.

I've managed to get the code to write to the highscore file:

ofstream file("highscore.txt");
file << score;

But I cant find out how I would load what i've saved into the variable highscore.

please help:)

J64

SonShadowCat
Member #1,548
September 2001
avatar

You just do the opposite. Open a file in output( I think) mode, then file >> score or whatever other variable you're using.

23yrold3yrold
Member #1,134
March 2001
avatar

Well, without outputting spaces, that won't go over well. :) But basically, yeah.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

jools64
Member #8,718
June 2007

could you give me the code to do that?
thanks:)

J64

LennyLen
Member #5,313
December 2004
avatar

jools64
Member #8,718
June 2007

I tried this:

string line;
ifstream file ("highscore.txt");
if (file.is_open())
{
while (! file.eof() )
{
getline (file,line);
textprintf_ex( buffer, font, 300, 300, makecol(255, 255, 255),-1, "Highscore: %d", line);
}
file.close();
}

It compiles fine but when I run it I get
Super Jet Pack Boy.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

J64

LennyLen
Member #5,313
December 2004
avatar

This probably isn't the source of the crash, but in the textprintf_ex() call, you tell it to expect an integer, and then pass a string.

Dustin Dettmer
Member #3,935
October 2003
avatar

string line;
ifstream file ("highscore.txt");
if (file.is_open())
{
while (! file.eof() )
{
getline (file,line);
textprintf_ex( buffer, font, 300, 300, makecol(255, 255, 255),-1, "Highscore: %s", line.c_str());
}
file.close();
}

jools64
Member #8,718
June 2007

Thanks Dustin Dettmer it worked =D

J64

Dustin Dettmer
Member #3,935
October 2003
avatar

To be fair you should be thanking LennyLen, as we pretty much said the same thing.

jools64
Member #8,718
June 2007

thanks LennyLen too, sorry I did'nt see your other reply.

J64

James Stanley
Member #7,275
May 2006
avatar

Don't use that in an Allegro program.
line is already declared by Allegro.

Timorg
Member #2,028
March 2002

This is the code I give my students to handle high scores.
http://www.timorg.net/high_score.zip
If you are interested but have problems, post here or msg me. :)

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

spellcaster
Member #1,493
September 2001
avatar

@Timorg: It's amazing how similar your code is to mine ;)
Just wondering: Why are you using so many c-isms in a c++ code? And if this code is intended to be an example, I'd suggest to avoid magic numbers in your code and either #define or const the appropriate numerals.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

James Stanley
Member #7,275
May 2006
avatar

Why would you ever use const?
Just wondering...

spellcaster
Member #1,493
September 2001
avatar

Quote:

Why would you ever use const?

Maybe to indicate a constant? It also makes your code more easy to read / maintain.
Or do you want to know when to use const instead of a define?

--
There are no stupid questions, but there are a lot of inquisitive idiots.

James Stanley
Member #7,275
May 2006
avatar

Yeah.
I don't see the advantage, surely define is faster since it's done by the preprocessor...?

spellcaster
Member #1,493
September 2001
avatar

Well, there shouldn't be a speed difference at all. The main reason to use const is that it is known at compile time. So you'll see the constants in error messages or while debugging.
Also, you can avoid some pitfalls using const:

#define FOO 10

/* oups... forgot to add brackets here, but who cares */
#define BAR FOO+1
#define BAZ 2* BAR

/* BAZ is now: 2 * 10 +1 = 21
 * instead of 2*BAR (= 2*11)
 */

You can avoid the problem if you place all your defines in () but this can be forgotten.

It's also considered the better "C++" style.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

BAF
Member #2,981
December 2002
avatar

Defines won't give an error (except for random screwed up ones) if you have a naming conflict whereas consts will.

Dustin Dettmer
Member #3,935
October 2003
avatar

IMO naming conflicts is the main reason to not use macros. Naming conflicts were sent here by the devil (just think of winalleg.h X[)

Timorg
Member #2,028
March 2002

spellcaster said:

@Timorg: It's amazing how similar your code is to mine ;)
Just wondering: Why are you using so many c-isms in a c++ code? And if this code is intended to be an example, I'd suggest to avoid magic numbers in your code and either #define or const the appropriate numerals

Our code being similar just means that it is being done in the right way. :P

I wrote that original code back in 2000 for a class assignment, It didn't have any error checking, it was just the basic code, and I shared it with the other people in the subject, 3 people got in trouble for plagarism, because they didn't site my code where everyone else did.

As for the C-isms, I guess I am a C programer at heart, but the code was for visual studio 6, and combining C and C++ code is a pain, so its basically C code in a cpp file. I only really use C++ for classes and the stl, everything else is done in C. I also don't really understand iostream for binary files, its just quicker and simpler for me to use the C functions to do it. And stl sorts arnt as simple as the qsort function.

As for magic numbers, yeah I should have used constants for the length, cause I remember old amiga games with scrolling highscores that go up from 100, and to change that you need to change values in a few places, where there should just be a const in the header.

____________________________________________________________________________________________
"c is much better than c++ if you don't need OOP simply because it's smaller and requires less load time." - alethiophile
OMG my sides are hurting from laughing so hard... :D

Go to: