Save and Load data not working properly

Hi, I've done this before but I can't seem to make it work now. I think it's because my load function is returning a struct now.

Does anybody have an idea on why this isn't working properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  void savedata(scrabble variable){
	int k, j;
	FILE *save;
	save = fopen("scrabble.txt", "w");
	fprintf(save, "%d\n", variable.n);
	fprintf(save, "%d\n", variable.p);
	fprintf(save, "%d\n", variable.no);
	for(k=1;k<=variable.no;k++){
		fprintf(save,"%c\n",variable.players[k].tiles);
		fprintf(save,"%d\n",variable.players[k].score);
	}
	for(k=1;k<=100;k++){
		fprintf(save, "%d\n", variable.letter[k]);
	}
	for(k=0; k<24; k++){
		for(j=0; j<=12; j++){
			fprintf(save, " %c", variable.board[k][j]);					
		}				
	}
	fclose(save);
}

scrabble loaddata(scrabble variable, int m){
	int k, j, l;
	FILE *load;
	if ((load=fopen("scrabble.txt", "r"))==NULL){
		variable.n=100;
		variable.p = 1;
		variable.no = 0;
		strcpy(variable.letter, "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ  "); 
		if(m==0){
			m=m+1;
			printf("No existing save game. Loading new game\n");
			printf("%d", m);
		}
	}else{		
		fscanf(load, "%d\n", &variable.n);
		fscanf(load, "%d\n", &variable.p);
		fscanf(load, "%d\n", &variable.no);
		for(k=1;k<=variable.no;k++){
			fscanf(load,"%c\n",variable.players[k].tiles);
			fscanf(load,"%d\n",&variable.players[k].score);
		}
		for(k=1;k<=100;k++){
			fscanf(load, "%d\n", variable.letter[k]);
		}
		for(k=0; k<24; k++){
			for(j=0; j<=12; j++){
				fscanf(load, " %c", variable.board[k][j]);				
			}				
		}
	}
	fclose(load);
	return variable;
}


This is the part where I call them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
printf("\nDo you wanna load previous game?\n");
	printf("[1]   Yes   \n");
	printf("[2]   No   \n");
	scanf("%d", &choice);
	if(choice<1 || choice>2){
		while(choice<1 || choice>2){
			printf("Invalid Input!");
			scanf("%d", &choice);
		}
	}
	if(choice==2){
		variable.n=100;
		variable.p = 1;
		variable.no = 0;
		strcpy(variable.letter, "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ  "); 
	}else{
			variable.p = loaddata(variable, m).p;
			variable.no = loaddata(variable, m).no;
			variable.n = loaddata(variable, m).n;
			variable.letter[101] = loaddata(variable, m).letter[101];	
			}


Also the part where I print "No existing save game exists", keeps repeating even if m is not equal to 0 anymore.
Anybody? Please, I need this for tomorrow
Anybody? Please, I need this for tomorrow
Anybody? Please, I need this for tomorrow
It is not considered nice to bump so quickly.


'm' is a local variable within loaddata. It is initialized with a copy of the value hold by the parameter that the function is called with. Call to loaddata does not modify 'm' of the callig code.

C++ allows passing parameters by-reference (both with type reference and with pointer) instead of by-value. C has the pointer option too.
Sorry, no the m that I'm using is the one on the main function I'm not using a nother m in the loaddata.

I've already did this formula of save and load b4 but I'm not sure why this one is not working.

And I also not counting on using pointers cause that always confuse me.
scrabble loaddata(scrabble variable, int m);
That 'm' is the one that you do use in loaddata.
I know, but the function is giving me a status access violation error.
It would help if we would know the definition of class scrabble.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
typedef struct{
	char tiles[8];
	int score;
}games;

typedef struct{
	int m;
	int n;
	int p; 
	int no;
	games players[4];	
	char letter[101];
	char board[24][12];
	
}scrabble;


And here I am calling them:
1
2
3
4
5
6
7
8
if(choice==2){
		variable.n=100;
		variable.p = 1;
		variable.no = 0;
		strcpy(variable.letter, "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ  "); 
	}else if(choice == 1){
			variable = loaddata(variable, m);	
			}


Please help me ASAP I need this by tomorrow
The valid indices (i.e. values of N) for char foo[N] are in range from 0 to N-1. You do access N, i.e. one past the end, with letter[101] and board[23][12].
That's where I initialized them, at the struct. I don't get it. I'll put my new way of calling them again and then try to look at my post above; the save and load data
1
2
3
4
5
6
7
8
if(choice==2){
		variable.n=100;
		variable.p = 1;
		variable.no = 0;
		strcpy(variable.letter, "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ  "); 
	}else if(choice == 1){
			variable = loaddata(variable, m);	
			}
Topic archived. No new replies allowed.