Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / File reading problem

Author
Message
Yero008
13
Years of Service
User Offline
Joined: 5th Dec 2010
Location: Romania
Posted: 3rd Jan 2011 14:42 Edited at: 3rd Jan 2011 21:06
I use this 2 commands to read and write the max score, but it gives me lots of errors.

What could I do?
Or how I could use the dark gdk functions to read from file.
Forward thanks.
Yero008
13
Years of Service
User Offline
Joined: 5th Dec 2010
Location: Romania
Posted: 3rd Jan 2011 20:49 Edited at: 3rd Jan 2011 21:06
Sorry for the double post.
puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 3rd Jan 2011 22:38
There are a few problems with your code.

First off, I'm not sure if you included fstream or not, but in order to use ifstream and ofstream you need to do that like



And unless you have put "using namespace std;" or "using std::ifstream" you need to change



to



Then, your function name has a space and has no paramter listing or ()'s, so that needs to be changed to



Finally, the while loop needs to have a condition inside the parentheses. You can use



Which will just keep reading until the end of the file.
Yero008
13
Years of Service
User Offline
Joined: 5th Dec 2010
Location: Romania
Posted: 3rd Jan 2011 23:15
I included the header, and i'm wrote using namespace std;
And when I compile it gives me this errors.
In the file is only one variable (score_max) and I read it and the I delete the file, in the game it makes the new score(new_score) and when the game end it compares the two and writes in the file which one is bigger.


Many thanks
puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 4th Jan 2011 02:28
Ah, I believe this can be solved by opening the project's properties (project->properties), then navigate through

configuration properties->C/C++->code generation

and set "Runtime library" to "Multi Threaded" or "MT"
Yero008
13
Years of Service
User Offline
Joined: 5th Dec 2010
Location: Romania
Posted: 4th Jan 2011 09:34
Thank you very much it works.
Yero008
13
Years of Service
User Offline
Joined: 5th Dec 2010
Location: Romania
Posted: 4th Jan 2011 21:25 Edited at: 4th Jan 2011 22:24
Now my file reading problem has turned into a writing one.
It reads my variable but, when the game ends, and the score is higher than the last one I want to delete everything in the file and write the new value. I use this code, but it doesn't write in the file.Also scor_max becomes scor and it's after the writing and this operation is done(scor_max=score),and in the file there's the old high score, the one I read at the begging, the current score is scor, and the high one read at the beggining is scor_max.

Forward thanks.
puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 4th Jan 2011 22:54
I believe when you open a file with ofstream, by default it will overwrite the contents of the file unless you set the write mode to std::ios::append or something like that, so I don't think there is any need to delete the file and then recreate it.

I also couldn't help but notice that you changed the filename from "scor.scor" to "scor.txt", could you be writing to the wrong file?

Lastly, many people on this forum have avoided using dbStr, as it allocates memory. You can replace it with sprintf or sprintf_s
Yero008
13
Years of Service
User Offline
Joined: 5th Dec 2010
Location: Romania
Posted: 4th Jan 2011 23:17 Edited at: 5th Jan 2011 15:43
I modified the file from "scor.scor" to "scor.txt".This is a portion of the code.I put the delete as a comment but still it doesn't modify the file.

void DarkGDK ( void )
{
//code
ifstream fin("scor.txt");
fin>>scor_max;
fin.close();
while ( LoopGDK ( ) )
{
//code
//colision check at the last level
and if it's true it verifies the score
if (scor>scor_max)
{
dbShowSprite(finish_max);
dbTextSize(32);
char szscor[ 256 ] = "";
strcpy ( szscor, "scor = " );
strcat ( szscor, dbStr ( scor ) );
dbText (dbScreenWidth()/2,dbScreenHeight()-200,szscor);
//dbDeleteFile("scor.txt");
ofstream fout("scor.txt");
fout<<scor;
scor_max=scor;
}
}

}

L.E: I tried modifying the output file to scor1.txt and it didn't create the file. What should I do?
L.L.E: Never mind I solved the problem.

Login to post a reply

Server time is: 2024-10-02 13:36:32
Your offset time is: 2024-10-02 13:36:32