fscanf from text file problems

The error i get is "Exception thrown at 0x0F6CB5F2 (ucrtbased.dll) in Mineseeper.exe: 0xC0000005: Access violation writing location 0xCDCDCDCD" which as far as i could research means i didn't have space to store what I was trying to read, but as far as i can see I've got that covered. Any suggestions?

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

typedef struct nodmain  //some blocks with the information i want to save
{
	nod *nextright;
	nodmain *nextdown;
	nodmain *prev;
	int num;
	bool mine;
	bool reveal;
};

typedef struct nod
{
	nodmain *prevmain;
	nod *prevnod;
	nod *nextright;
	int num;
	bool mine;
	bool reveal;
};

//functions to initialize the structs so i can be lazy and just call them :D

nod *Inicializa() //me being lazy
{
	nod *init = (nod*)malloc(sizeof(nod));
	return init;
}

nodmain *Inicializamain() //me being lazy x2
{
	nodmain *init = (nodmain*)malloc(sizeof(nodmain));
	return init;
}


 nodmain *Load(int size)
{
	FILE *Load = fopen("SavedGame.txt", "r");  //the text already exists
	nodmain *auxmain = Inicializamain();
	nodmain *header = auxmain;
	char boolstringmine[10];
	char boolstringreveal[10];
	
	for (int i = 0; i < size; i++)
	{
		fscanf(Load, "%d,%s,%s\t", auxmain->num, &boolstringmine, &boolstringreveal);  //ERROR here
		if (boolstringmine == "true")
		{
			auxmain->mine = true;
		}
		else
		{
			auxmain->mine = false;
		}
		if (boolstringreveal == "true")
		{
			auxmain->reveal = true;
		}
		else
		{
			auxmain->reveal = false;
		}
		nod *aux = Inicializa();
		auxmain->nextright = aux;
		aux->prevmain = auxmain;
		aux->prevnod = NULL;
		for (int j = 0; j < (size - 2); j++)
		{
			fscanf(Load, "%d,%s,%s\t", aux->num, &boolstringmine, &boolstringreveal);
			if (boolstringmine == "true")
			{
				aux->mine = true;
			}
			else
			{
				aux->mine = false;
			}
			if (boolstringreveal == "true")
			{
				aux->reveal = true;
			}
			else
			{
				aux->reveal = false;
			}
			nod *nextnod = Inicializa();
			nextnod->prevnod = aux;
			nextnod->prevmain = NULL;
			aux->nextright = aux;
			aux = aux->nextright;
		}
		nodmain *nextmain = Inicializamain();
		auxmain->nextdown = nextmain;
		nextmain->prev = auxmain;
		auxmain = nextmain;
		fscanf(Load, "\n");
	}
	
	fclose(Load);

	return header;
}

//this function is the one that writes on the text file, works perfectly.
void Save(nodmain *header)
{
	FILE *Save = fopen("SavedGame.txt", "w");
	nodmain *auxmain = header;
	nod *aux = Inicializa();

	if (Save == NULL)
	{
		printf("ERROR");
	}
	else
	{
		while (auxmain != NULL)
		{
			fprintf(Save, "%d,%s,%s\t", auxmain->num, auxmain->mine ? "true" : "false", auxmain->reveal ? "true" : "false");
			aux = auxmain->nextright;
			while (aux != NULL)
			{
				fprintf(Save, "%d,%s,%s\t", aux->num, aux->mine ? "true" : "false", aux->reveal ? "true" : "false");
				aux = aux->nextright;
			}
			fprintf(Save, "\n");
			auxmain = auxmain->nextdown;
		}
	}
	fclose(Save);
}
fscanf(Load, "%d,%s,%s\t", auxmain->num, &boolstringmine, &boolstringreveal); //ERROR here

fscanf expects a pointer but you pass the address of an pointer &boolstringmine
Try either &boolstringmine[0] OR boolstringmine same for &boolstringreveal
fscanf(Load, "%d,%s,%s\t", auxmain->num, &boolstringmine, &boolstringreveal); //ERROR here

auxmain->num is an int. You're supposed to give the address of that int. You also seem to be giving the address of a pointer to boolstringmine and boolstringreveal, instead of the addresses of those arrays.

If you used C++ instead of C, this would be much easier and much less prone to error.

//functions to initialize the structs so i can be lazy and just call them :D
We have those in C++. They're called constructors. If you're reimplementing, badly, things that C++ comes with, you really should consider moving to C++.


fscanf(Load, "\n");
What's this trying to do?
Last edited on
0xCDCDCDCD is a so called magic number. See:

https://en.wikipedia.org/wiki/Magic_number_(programming)

Used by Microsoft's C/C++ debug malloc() function to mark uninitialized heap memory, usually returned from HeapAlloc()


It actually means there is at least one uninitialized member.
Changed to :fscanf(Load, "%d,%s,%s\t", &num, boolstringmine, boolstringreveal);
(then aux->num=num and same for auxmain->num)
It now reads from the text file, however the "%s" reads until there's no more space to store, instead of reading till a comma, which is what i'm trying to achieve.

"fscanf(Load, "\n");
What's this trying to do?" -> I put "\n" to change lines in the save function (easier to confirm the information on the text file), so i have to let the reader function know its supposed to change lines, am i wrong?







Thank you that solved the comma problem ^~^
However something else came up...
When scanning the string until commas it scans something like "true\0?????", but if i compare string=="true" it returns false? Sorry if I'm having way too many doubts I'm new to programming >.<
You can't compare them with ==, this works only for C++ strings. You need to use strcmp
http://www.cplusplus.com/reference/cstring/strcmp/
Thank you everything seems to be working perfectly know ^~^
Topic archived. No new replies allowed.