getline with fstream crashing program

ok, I have this part of my 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
33
34
35
36
37
38
if(file.is_open())
	{
		char* temp = "";
		file.ignore( numeric_limits<streamsize>::max(),'[');
		file.getline(temp,numeric_limits<streamsize>::max(),']');
		title = temp;
		file.ignore( numeric_limits<streamsize>::max(),'[');
		file.getline(temp,numeric_limits<streamsize>::max(),']');
		backgPic = temp;

		file.ignore( numeric_limits<streamsize>::max(),'[');
		file.getline(temp,numeric_limits<streamsize>::max(),']');
		int result = 0;
		stringstream h(temp);
		if(!h >> result)
		{
			result = 1024;
			h.clear();
		}
			
		width = result;

		stringstream t(temp);
		file.ignore( numeric_limits<streamsize>::max(),'[');
		file.getline(temp,numeric_limits<streamsize>::max(),']');
		if(!h >> result)
		{
			result = 576;
			h.clear();
		}
		height = result;

		file.ignore( numeric_limits<streamsize>::max(),'[');
		file.getline(temp,numeric_limits<streamsize>::max(),']');
		font = temp;
		return;

	}

It compiles correctly, but crashes at runtime. I have debugged and found that the problem was with file.getline(temp,numeric_limits<streamsize>::max(),']');
I changed the numeric_... max() to 100 and it did the same. I changed the delim char. Did the same. So I dont know what the problem is.

The exact error is: SFML2.exe has stopped working. Windows will close the program and notify you if a...

Not very descriptive, but that is what I got.

Thanks
Last edited on
Try declaring temp as std:string instead of char*.
getline(file, temp, ']'); I used this with temp as a string and It worked!
thanks
Last edited on
New problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
file.ignore( numeric_limits<streamsize>::max(),'[');
getline(file, temp, ']');
int result = 0;
stringstream h(temp);
if(!h >> result)
{
	result = 1024;
	h.clear();
}
			
width = result;
cout << result <<endl;

stringstream t(temp);
file.ignore( numeric_limits<streamsize>::max(),'[');
getline(file, temp, ']');
if(!h >> result)
{
	result = 576;
	h.clear();
}
height = result;
cout  << result << endl;


All of the inputs work correctly except this one. when I look at the result with cout, they are both 0, even when the numbers between the brackets were not. Do you know why?
Last edited on
I fixed it. The problem was that the string was not getting turned into an int properly. I ended up using atoi();

Thanks abhishekm!
Topic archived. No new replies allowed.