Thank you Elysia for the very nice explanation. ![Smile](https://cboard.cprogramming.com/images/smilies/smile.png)
Seems I'm on a roll here, so perhaps you wouldn't mind helping me some more.
I need to write the array to a file, but I can't get the function to work. I know it opens right because the LoadHighscore function works and I get no error messages when trying to open the file in SaveHighscore. I've also checked that the file is actually write enabled.
I've also added another array along the lines of highscore[] called test so that I'll know it works properly if it's printed with the LoadHighscore (which should mean that the highscore array has been replaced with the test one). Anyway, that's all academic as I can't get it to write in the first place.
highscore.c with the SaveHighscore function
Code:
// open file
int SaveHighscore(int *test)
{
FILE *fil;
fil = fopen("highscore.txt", "w");
if (fil == NULL) {
printf("Unable to open file\n");
goto error;
}
if (fwrite(&test, sizeof(test[0]), 11, fil) != 1){
printf("Unable to write to file\n");
goto error;
}
fclose(fil);
return 1;
error:
return 0;
}
void LoadHighscore(int *highscore)
{
FILE *stream;
int i;
stream = fopen("highscore.txt", "r");
if(stream==NULL) {
printf("Error: can't open file.\n");
}
else {
printf("File opened.\n");
}
fread(highscore, sizeof(highscore), 10, stream);
for(i=0; i<10; i++){
printf("File read: %d \n", highscore[i]);
}
fclose(stream);
}
main.c
Code:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <assert.h>
#include "SDL.h"
#include "SDL_ttf.h"
#include "drawline.h"
#include "triangle.h"
#include "teapot_data.h"
#include "sphere_data.h"
#include "object.h"
#include "text.h"
#include "list.h"
#include "highscore.h"
int highscore[11] = {1,1,2,3,4,5,6,7,8,9,9};
int test[11] = {2,2,2,2,2,2,2,2,2,2,2};
(snip)
// Start game
while (1) {
SaveHighscore(test);
LoadHighscore(highscore);
If I move the entire int SaveHighscore into main and change the 3rd argument in fwrite from 11 to 1 it'll print out
File opened.
File read. 4342656
File read: 1
File read: 2
etc.
What's that all about?
Any suggestions?