input problem/fscanf to int doesn't work correctly help please(fixed)

Hello,
I have tried to make a fileloader function but it didn't worked.
I have searched this problem on google but it didn't help.

The load function
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
bool Game::LoadGame(int LoadNum)
{
	//maken
	char* FileBaseName = "Save";
	char buffer[64];
	
	sprintf(buffer,"%s%.1d.sav",FileBaseName,LoadNum);
	/*std::ifstream Load ( buffer );
	This is my first attempt but it didn't work
	Load>>Year;
	Load>>Month;
	Load>>Day;
	Load>>Hour;
	Load>>Minute;
	Load>>Money;
	
	
	//Load>>Stock[0][0];*/
        //This is my second attempt and it still doesn't work
        //I have tried to change the %i to %d and
        //a few others but that also didn't solve the problem
	FILE* Load = fopen(buffer, "r");	
	fscanf(Load,"%i",&Year);
	fclose(Load);
        //for now it just loads the Year value but later it will load the rest of the value's
	

	if(Year<=100&&Year>=0)return true;
	else return false;


}

When I debug it the value of Year doesn't change at all.

This is the way how it is saved.
This is the Save funtion
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
void Game::SaveGame(int SaveNum, bool ngame)
{
	char* FileBaseName = "Save";
	char buffer[64];
	
	sprintf(buffer,"%s%.1d.sav",FileBaseName,SaveNum);
	std::ofstream SaveFile(buffer, std::ofstream::out);
	if(ngame)
	{
		Year=Month=Day=1;
		Hour=8;
		Minute=0;
		Money=3000;
		
		
	}
	
	SaveFile << Year << "\n";
	SaveFile << Month << "\n";
	SaveFile << Day << "\n";
	SaveFile << Minute << "\n";
	SaveFile << Money << "\n";
		
	
	
	SaveFile.close();
	
}

I use the 1st save named Save1.sav and the save function works.

I don't need to finish the code anytime soon but the sooner it works the better.
Thanks for reading.
Last edited on
If it can be done an other way that is also good.
It doesn't have to be with scanf.
Are you sure the filename is correct when you open the file for input?
Regardless of whether the C or C++ code is used, you should check that the file is successfully opened before attempting to read from it.
Thanks for replying Chervil,
The save file is called Save1.sav or with any other number behind it, i have tried to directly open "Save1.sav"instead of using the buffer but it also didn't work.
The only thing that changes in the values is the adress but it changes to something fixed random so it is always the same value.

my new code is now
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
bool Game::LoadGame(int LoadNum)
{
	//maken
	char* FileBaseName = "Save";
	char buffer[64];
	
	sprintf(buffer,"%s%.1d.sav",FileBaseName,LoadNum);
	/*std::ifstream Load ( buffer );
	
	Load>>Year;
	Load>>Month;
	Load>>Day;
	Load>>Hour;
	Load>>Minute;
	Load>>Money;
	
	
	//Load>>Stock[0][0];*/
        //to be sure i changed the buffer to Save1.sav but it still doesn't work
	FILE* Load = fopen("Save1.sav", "r+");
	if(Load)
	{	
		fscanf(Load,"%s",&Year); //it loads in the debugger but still the value doesn't change
		fclose(Load);
	}
	else return false;
	

	if(Year<=100&&Year>=0)return true;
	else return false;


}

I have now implented a test but it opens the file but doens't read it.
Thanks for reading and helping.
The thing is, I took your original code and tested it.

Since I had no way of knowing what values you were passing as the parameters int LoadNum and int SaveNum, bool ngame
I removed the part where the filename is constructed and just used a single fixed filename for both input and output. I also set bool ngame to true.

What I found was that your code worked correctly, including a second test with the code which had been commented out. At this stage I am at a loss to suggest what is going wrong. I've a feeling there is some information missing here which will provide the answer.

Can you explain the comment in the code at line 23, //it loads in the debugger but still the value doesn't change please? The comment seems to contradict itself.

For example, where is Year defined? Are you sure that the variable accessed in these functions is the same variable as the one which which "doesn't change"?
Thanks for the reply,
I just tried to make an Int in the function and then read the value into that int and it Worked but if I try Year again it doesn't.
I also have tried to int the Year as year (current code) but it didn't worked
First the int of year was:
int Year,Month,Day,Hour,Minute;

This is the Game.h file:
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#pragma once

#include "D3DGraphics.h"
#include "Keyboard.h"
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <sstream>

#include "Mouse.h"
#include "Timer.h"
#include "Sound.h"
#define MAXITEMS 20
#define MAXCITY 6

#define DINGWIDTH 50
#define DINGHEIGHT 50
#define DINGNFRAMES 14
#define DINGFRAMETIME (60 / DINGNFRAMES)
#define DINGFILENAME "plaatjes\\ding"







class Game
{
public:
	struct AnimatedSpriteTemplate
	{
		Sprite* frames;
		int nFrames;
		int frameDuration;
	};
	struct AnimatedSpriteInstance
	{
		AnimatedSpriteTemplate* templat;
		int currentFrame;
		int currentFrameExposure;
		float x,y;
	};


public:
	Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer );
	~Game();
	void LoadAnimatedSprite(AnimatedSpriteTemplate* templat,  
		const char* basename, int width, int height, int nFrames, int frameDuration);
	void FreeAnimatedSprite( AnimatedSpriteTemplate* templat);
	void CreateSpriteInstance(AnimatedSpriteTemplate* templat, AnimatedSpriteInstance* instance);
	void UpdateAnimation(AnimatedSpriteInstance* instance);
	void DrawSpriteInstance( AnimatedSpriteInstance* instance);
	bool QuitGame;


	void Go();
private:
	void ComposeFrame();
	
	//composeframe
	void DrawScreen();
	
	//sprite draw
	void CloudsDraw();
	void Background();
	void RoadDraw();
	void HouseBackDraw();
	void ObjectsDraw();
	void CharactersDraw();
	void HouseFrontDraw();
	void MouseDraw();
	
	//save/load
	void SaveGame(int SaveNum, bool ngame);
	bool LoadGame(int Loadnum);
	
	//animatie
	void MoveUpdate();
	
	//camera
	void CamUpdate();
	
	//player interface
	void Gravity();
	void Interface();
	void MoveInterface();
	void CityTravelInt();
	
	
	//menu's tekenen
	void OnscreenDraw();
	void IngameMenuDraw();
	void HeadMenuDraw();
	void ErrorScreen(char* Error);
	
	//menu's interface
	void MenuActionHandler();
	void SaveGameMenu();
	void SaveGameMenuInt();
	void HeadMenuInt();
	void OptionsInt();
	void LoadMenuInt();
	void IngameMenuInt();	
	void ErrorMenuInt();
	
	//tijd
	void ConvertTime();
	void DrawTime();
	void TimeUpdate();
	
	//handel AI
	void GiveGoodsNames();
	int Price(int GoodsNum, bool BuySell);
	/********************************/
private:
	D3DGraphics gfx;
	KeyboardClient kbd;
	MouseClient mouse;
	DSound audio;
	
	/********************************/
	//prijs
	float StartPrice[MAXITEMS];
	float Inflation[MAXCITY];
	int Money;
	//steden
	int City;
	
	
	//ingame menu check
	bool Menu;
	/*stats & skills*/
	int Pskills[10],Sskills[10],stats[5];
	
	//gravity system player
	bool Ajump, Djump,Afall, AVelocity, Amove,AmoveY;
	int PlayerX, PlayerY;
	float XVelocity, YVelocity;
	//animation state player
	int MoveState;
	int MoveStateDelay;

	//begin menu
	bool GameStarted, savegame,optionsmenu,loadgame,credits;
	int pressdelay;
	bool PressCheck;
	bool MenuButtonPressed[5];
	
	//error menu check
	bool errorscreen;
	char* Error;
	bool ErrorMenuPressed;
	//bmp pictures
	//lul figuur
	AnimatedSpriteTemplate dingTemplate;
	AnimatedSpriteInstance ding;
	//background
	Sprite BackgroundS;
	//character
	Character Farie;
	//font
	Font fixedSys;
	//menu
	MenuButton HeadMenu;
	MenuButton IngameMenu;

	//camera
	int XCam,YCam;
	int Height, Width;

	//time
	char* Years;
	char* Months;
	char* Days;
	char* Hours;
	char* Minutes;
	int TimeSpeed;
	bool timercheck;
	int timebuffer;
	int timesec;
	Timer timer;
	
	//goods
	char* Goods[10];
	int PlayerStock[MAXITEMS];
	int Stock[MAXITEMS][MAXCITY];
	
	//test
	

	//quit
	
	/********************************/
};


I defined the Year in top of the code as -1 but this also didn't change.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Game::Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer )
:	gfx( hWnd ),
	audio( hWnd ),
	kbd( kServer ),
	mouse( mServer ),
	Menu(false),
	savegame(false),
	loadgame(false),
	optionsmenu(false),
	credits(false),
	pressdelay(0),
	GameStarted(false),
	QuitGame(false),
	PressCheck(true),
	errorscreen(false)
	
	
	
{
	year=-1;
       //more code but it doesn't matter here
}


And this is the code that is important with the loading procces:

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//the code that calls the function
void Game::LoadMenuInt()
{
	int xmouse = mouse.GetMouseX();
	int ymouse = mouse.GetMouseY();
	if(mouse.LeftIsPressed()&&pressdelay==0&&PressCheck)
	{
		if(xmouse>=300&&xmouse<=500)
		{
			if(ymouse<=125&&ymouse>=75)
			{
				MenuButtonPressed[0]=true;
					
			}
			if(ymouse<=225&&ymouse>=175)
			{
				MenuButtonPressed[1]=true;
					
				
			}
			if(ymouse<=325&&ymouse>=275)
			{
				MenuButtonPressed[2]=true;
					
			}
			if(ymouse<=425&&ymouse>=375)
			{
				MenuButtonPressed[3]=true;
					
			}
		}
	}
	if(!mouse.LeftIsPressed())
	{
		if(xmouse>=300&&xmouse<=500)
		{
			if(ymouse<=125&&ymouse>=75&&MenuButtonPressed[0])
			{
				MenuButtonPressed[0]=false;
				if(LoadGame(1))
				{
					GameStarted=true;
					loadgame=false;
				}
				else 
				{
					errorscreen=true;
					Error="Save doesn't exist.";

				}
			}
			if(ymouse<=225&&ymouse>=175&&MenuButtonPressed[1])
			{
				MenuButtonPressed[1]=false;
				if(LoadGame(2))
				{
					GameStarted=true;
					loadgame=false;
				}
				else 
				{
					errorscreen=true;
					Error="Save doesn't exist.";

				}
			}
			if(ymouse<=325&&ymouse>=275&&MenuButtonPressed[2])
			{
				MenuButtonPressed[2]=false;
				if(LoadGame(3))
				{
					GameStarted=true;
					loadgame=false;
				}
				else 
				{
					errorscreen=true;
					Error="Save doesn't exist.";

				}
			}
			if(ymouse<=425&&ymouse>=375&&MenuButtonPressed[3])
			{
				MenuButtonPressed[3]=false;
				loadgame=false;	
			}
		}
		for(int index=0; index<=4; index++)
		{
			MenuButtonPressed[index]=false;
		}
	}

}
//the save function when it is called the SaveNum is 1 and ngame is true
void Game::SaveGame(int SaveNum, bool ngame)
{
	char* FileBaseName = "Save";
	char buffer[64];
	
	sprintf(buffer,"%s%.1d.sav",FileBaseName,SaveNum);
	std::ofstream SaveFile(buffer, std::ofstream::out);
	if(ngame)
	{
		year=Month=Day=1;
		Hour=8;
		Minute=0;
		Money=3000;
		
		
	}
	
	SaveFile << year << "\n";
	SaveFile << Month << "\n";
	SaveFile << Day << "\n";
	SaveFile << Minute << "\n";
	SaveFile << Money << "\n";
		
	
	
	SaveFile.close();
	
}
//the LoadGame for now I only use LoadNum=1
bool Game::LoadGame(int LoadNum)
{
	//maken
	char* FileBaseName = "Save";
	char buffer[64];
	
	sprintf(buffer,"%s%.1d.sav",FileBaseName,LoadNum);
	/*std::ifstream Load ( buffer );
	again the old code i used
	Load>>year;
	Load>>Month;
	Load>>Day;
	Load>>Hour;
	Load>>Minute;
	Load>>Money;
	
	
	//Load>>Stock[0][0];*/
	
	FILE* Load = fopen("Save1.sav", "r");
	if(Load)
	{	
		fscanf(Load,"%d",&year);
		fclose(Load);
	}
	else return false;
	

	if(year<=100&&year>=0)return true;
	else return false;


}


Thank you very much for helping as I can't find a sollution.

(edit) I think my memory is full is there a way to see how much you use?
Last edited on
Thanks Chervil it works now it was a random bug in the debugger but now it is working.
(closed)
Thanks for the update.
Topic archived. No new replies allowed.