Access violation in getline

Hi, it's me again! This time I have a error that occurs while running. I get access violation while trying to read from a file (It is created, and not blank!).
This is detailed error:
1
2
3
4
TXT_RPG.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_ru_b77a5c561934e089\mscorlib.resources.dll'
A first chance exception of type 'System.AccessViolationException' occurred in TXT_RPG.exe
'TXT_RPG.exe': Loaded 'C:\Windows\SysWOW64\version.dll', Cannot find or open the PDB file.
An unhandled exception of type 'System.AccessViolationException' occurred in TXT_RPG.exe 

And here is the game code:
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
#include <iostream>
#include <cmath>
#include <string>
#include <Windows.h>
#include <data.h>
#include <fstream>

#define DEBUG

using namespace std;

bool loaded;
int percent_loaded;
string action;
ofstream save;
ifstream load;
int loaded_health;

character hero;

int GAME();
void v_loaded();
void savegame();
void loadgame();

void Game_INIT()
{
	SetConsoleTitle("Loading...");

	for (percent_loaded; percent_loaded <= 100; percent_loaded += 25)
	{
		system("cls");
		cout << "Loading..." << endl;
		cout << percent_loaded << "% loaded." << endl;
		if (percent_loaded == 100)
		{
			v_loaded();
		}
	}
}

int GAME()
{
	hero.attribute[character::HEALTH] = 1;
	SetConsoleTitle("TXT - RPG");
#ifdef DEBUG
	hero.attribute[character::HEALTH] = 10;
#endif

	loadgame();

	cout << hero.attribute[character::HEALTH] << endl;
	Sleep(1000);
	loaded = true;
	cin >> action;
	if (action == "exit")
	{
		savegame();
	}
	return 1;
}

void v_loaded()
{
	system("cls");
	cout << "Loaded succeded!" << endl;
	Sleep(1000);
	GAME();
}

void savegame()
{
	save.open("save_g.sav", ios::out | ios::app | ios::binary);
	save << loaded << endl;
	save << hero.attribute[character::HEALTH] << endl;
	save.close();
}

void loadgame()
{
	load.open("save_g.sav", ios::in | ios::app | ios::binary);
	load.getline("load", loaded);
	load.getline("load", loaded_health);
	hero.attribute[character::HEALTH] = loaded_health;
	load.close();
}

What is the problem?
It's probably because you're trying to write to readonly memory. On lines 82-82 getline() wants a pointer to char where it will save the data it reads, but you're passing a string literal, which is const.
http://www.cplusplus.com/reference/istream/istream/getline/
It's probably because you're trying to write to readonly memory. On lines 82-82 getline() wants a pointer to char where it will save the data it reads, but you're passing a string literal, which is const.
http://www.cplusplus.com/reference/istream/istream/getline/

Now I see the problem, but I don't really understand how to solve it, so it will read the first line and save it as the value of bool loaded, and then read the second line and save it as the value of int loaded_health.
The point is that the first argument to ifstream::getline() must be a pointer to some memory that will store the characters that are read from the stream. And the second argument must be a maximum number of characters to read from the stream.

Why are you trying to pass in a string literal as that argument? What is that string "load" that you're passing in supposed to mean?
Last edited on
Oh, got it! Thanks, and sorry for the slow reaction (i am very tired today).
Topic archived. No new replies allowed.