Well, either you lost me somewhere along the very short road, or something else is not as it should be, because I still can't get it to write properly.
Well, either you lost me somewhere along the very short road, or something else is not as it should be, because I still can't get it to write properly.
Hehe, I'm sure you can punch many, and you're welcome to as long as I can patch them up again.
I didn't change much from the code I posted aside from the two you pointed out. Basically
andCode:if (fwrite(test, sizeof(test[0]), 11, fil) != 1){
Code:fread(highscore, sizeof(highscore[0]), 11, stream);I'm not clear on what you meant with this, so I didn't change it. I thought declaring the size of the element is what I wanted to do.Secondly, you tell fwrite to write the size of the type it points to, since all elements in an array are the same size, test[0] is the same size as test[1], etc.
And that size is not necessarily the size of a pointer. Oh no. Undefined behavior right there.
Finally, something I can fix.But that doesn't help us know what you've changed, from which version of what was posted, etc
highscore.h
highscore.cCode:#ifndef HIGHSCORE_H_ #define HIGHSCORE_H_ extern int highscore[]; extern int test[]; //void InsertScore(int *highscore); int SaveHighscore(int *test); void LoadHighscore(int *highscore); #endif /*HIGHSCORE_H_*/
main.cCode:#include <stdio.h> #include <stdlib.h> #include <errno.h> #include "highscore.h" // open file int SaveHighscore(int *test) { FILE *fil; fil = fopen("highscore.txt", "w"); if (fil == NULL) { printf("Unable to open file\n"); goto error; } fwrite(test, sizeof(test[0]), 11, fil); 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[0]), 11, stream); for(i=0; i<11; i++){ printf("File read: %d \n", highscore[i]); } fclose(stream); }
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" (snip) 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) int main(int argc, char **argv) { // Start game while (1) { SaveHighscore(test); LoadHighscore(highscore);
A few security addments, a few fixes and indentation fixing.
Highscore.h:
Highscore.c:Code:#ifndef HIGHSCORE_H_ #define HIGHSCORE_H_ //Avoid global variables //extern int highscore[]; //extern int test[]; int SaveHighscore(int* test, int size); void LoadHighscore(int* highscore, int size); #endif /*HIGHSCORE_H_*/
Main.c:Code:#include <stdio.h> #include <stdlib.h> #include <errno.h> #include "highscore.h" // open file int SaveHighscore(int* test, int size) { FILE* fil = fopen("highscore.txt", "w"); if (fil == NULL) { printf("Unable to open file\n"); return 0; } //fwrite(test, sizeof(test[0]), 11, fil); if (fwrite(test, sizeof(test[0]), size, fil) != 1) { printf("Unable to write to file\n"); return 0; } fclose(fil); return 1; } void LoadHighscore(int* highscore, int size) { FILE* stream = fopen("highscore.txt", "r"); if(stream == NULL) { printf("Error: can't open file.\n"); return; } else { printf("File opened.\n"); } fread(highscore, sizeof(highscore[0]), size, stream); for (int i = 0; i < 11; i++) { printf("File read: %d \n", highscore[i]); } fclose(stream); }
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}; int main(int argc, char** argv) { // Start game for(;;) { SaveHighscore(test, sizeof(test) / sizeof(test[0])); LoadHighscore(highscore, sizeof(highscore) / sizeof(highscore[0])); } }
Thanks for all the help, it's muchly appreciated. I finally got it to work.