|
reading text file |
jools64
Member #8,718
June 2007
|
hey, I've managed to get the code to write to the highscore file: ofstream file("highscore.txt"); But I cant find out how I would load what i've saved into the variable highscore. please help:)
|
SonShadowCat
Member #1,548
September 2001
|
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
|
Well, without outputting spaces, that won't go over well. But basically, yeah. -- |
jools64
Member #8,718
June 2007
|
could you give me the code to do that?
|
LennyLen
Member #5,313
December 2004
|
jools64
Member #8,718
June 2007
|
I tried this: string line; It compiles fine but when I run it I get
|
LennyLen
Member #5,313
December 2004
|
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.
|
ImLeftFooted
Member #3,935
October 2003
|
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
|
ImLeftFooted
Member #3,935
October 2003
|
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.
|
James Stanley
Member #7,275
May 2006
|
Don't use that in an Allegro program. |
Timorg
Member #2,028
March 2002
|
This is the code I give my students to handle high scores. ____________________________________________________________________________________________ |
spellcaster
Member #1,493
September 2001
|
@Timorg: It's amazing how similar your code is to mine -- |
James Stanley
Member #7,275
May 2006
|
Why would you ever use const? |
spellcaster
Member #1,493
September 2001
|
Quote: Why would you ever use const?
Maybe to indicate a constant? It also makes your code more easy to read / maintain. -- |
James Stanley
Member #7,275
May 2006
|
Yeah. |
spellcaster
Member #1,493
September 2001
|
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. #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. -- |
BAF
Member #2,981
December 2002
|
Defines won't give an error (except for random screwed up ones) if you have a naming conflict whereas consts will. |
ImLeftFooted
Member #3,935
October 2003
|
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 Our code being similar just means that it is being done in the right way. 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. ____________________________________________________________________________________________ |
|