.substr having trouble with spaces

Hello, I'm working on a text-based game, but I'm having some trouble with importing some data from a textfile...
for some reason it all works fine without spaces, but if I incclude a space in the name(in the textfile) it just breaks...
any help?
1
2
3
4
5
6
7
8
9
10
11
	string chardat = charload();
	int namelength;
	(stringstream)chardat.substr(0, 2) >> namelength;
	string name = "";
	name = chardat.substr(5, namelength);
	int strength;
	(stringstream)chardat.substr(8 + namelength, 2) >> strength;
	int speed;
	(stringstream)chardat.substr(13 + namelength, 2) >> speed;
	int HP;
	(stringstream)chardat.substr(18 + namelength, 2) >> HP;
You will need to show more of the code for us to help. How about showing charload() and also show where chardat is saved. And anywhere namelength is calculated would be nice too.
The namelength is calculated at the first time the name is put into the savefile.
this is that part of 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
int length = name.length();
	string namelength;
	ostringstream convert;
	convert << length;
	namelength = convert.str();
	if (length < 10)
		namelength = "0" + namelength;
	if (klasse == "ridder")
	{
		sstrength = "10";
		sspeed = "05";
		smorale = "10";
		sHP = "25";
		sklasse = "1";
	}
	else if (klasse == "jager")
	{
		sstrength = "06";
		sspeed = "09";
		smorale = "00";
		sHP = "20";
		sklasse = "2";
	}
	else if (klasse == "magier")
	{
		sstrength = "03";
		sspeed = "04";
		smorale = "06";
		sHP = "20";
		sklasse = "3";
	}
	else
		cout << "error";

	charsave = namelength + "-_-" + name + "-_-" + sstrength + "-_-" + sspeed + "-_-" + sHP + "-_-" + smorale;
	savechar(charsave);

then this is charsave:
1
2
3
4
5
6
7
void savechar(string data)
{
	ofstream saver;
	saver.open("chardat.txt", fstream::out);
	saver << data;
	saver.close();
}

then this is what's in chardat.txt:
15-_-BramiusHeldius!-_-10-_-05-_-25-_-10
then this is charload:
1
2
3
4
5
6
7
8
string charload()
{
	fstream loader;
	loader.open("chardat.txt", fstream::in);
	string data;
	loader >> data;
	return data;
}

and that all works just fine and looks like it should. but if I use Bramius Heldius as name it fails, I hope you can help
Where does name come from on line 1?
Name is a String that has been entered by the program user, it's obtained by the usage of getline(cin, name)
The charload() is where it fails. You use >> instead of getline.
the problem with that would be that the length of chardat.txt is variable, because the length of the name differs per user(different users enter different names)...
I will illustrate an important difference between >> and getline:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main() {
    
//your version
    std::fstream loader("file1.txt");
    std::string data;
    loader >> data;
    loader.close();
    std::cout << "v1: " << data << "\n";

//my version
    loader.open("file1.txt");
    getline(loader, data);
    std::cout << "v2: " << data << "\n";
    
    return 0;
}
v1: 16-_-Bramius
v2: 16-_-Bramius Heldius!-_-10-_-05-_-25-_-10

file1.txt:
16-_-Bramius Heldius!-_-10-_-05-_-25-_-10
Last edited on
Topic archived. No new replies allowed.