So, I wrote a C++ program in Visual studio 2010 in the form of a dice gambling game. Everything about the game works fine, however I wanted a way to store the high score between sessions, so I thought I'd use file I/O to store the high score. This is my snippet (function) for reading from my file.
// Opens and reads from the file containing high scores.
double readScores(void)
{
double score;
FILE *highscores;
highscores = fopen("highscores.txt", "r");
if(highscores == NULL) // make sure file opened properly.
{
printf("Sorry, highscores could not be loaded.\n");
return 1.0;
}
fscanf(highscores, "%lf",&score);
printf("The current high score is %.2f!!!\n",score);
fclose(highscores);
return score;
}
I also have a function to overwrite the new highscore to the same file, but that is not where my problem lies.
When I debug the program in visual studio, everything works fine and the program correctly displays the highest previous score, however when I try to run the program from the .exe file in my project Debug folder of visual studio, it cannot open the "highscores.txt" file and displays my error message. I've saved and built the project, so there's no reason one should work and the other shouldn't.
Any thoughts on why this would be or how I could fix it?
Thanks, I wrote the program so that it would create the file highscores.txt where it needed it, however apparently the debug application right in visual studio runs from a different directory than the .exe file of the program, so I needed to copy the file to an adjacent directory.