Help parsing a file

Hello,

I want to read escape characters from a file to format the output of text in a shell. The format is [Key Value] and the contents of the file are:
1
2
3
4
5
6
7
8
Black \033[0;30m
Red \033[0;31m
Green \033[0;32m
Brown \033[0;33m
Blue \033[0;34m
Purple \033[0;35m
Cyan \033[0;36m
Grey \033[0;37m


with the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
map<string, string> getEscapeMap(string fileName) {
	map<string, string> tmp;
	
	ifstream in;
	in.open(fileName.c_str());
	string line = "";
	
	while(!in.eof()) {
		getline(in, line);
		istringstream iss(line);
		string key, value;
		iss >> key;
		iss >> value;
		if(tmp.find(key) == tmp.end() && value != "")
			tmp.insert(pair<string, string>(key, value));
	}
	in.close();
	
	return tmp;
}


But, when I print them back out to cout, it prints the escape character instead of using the character to format the text(ie it prints "\033[0;37m", instead of changing the color of the text to gray. If I print the same escape character using a string constant(manually), it formats the text.

What am I missing here?
\ is an escape character in C++ string literals. \033 stands for ascii 27, so that's the character you have to use in the file - literally writing \033 won't do.
Or write code that converts the literal \033 to a number 0x33. (Maybe I've confused hex there?)
Last edited on
Yeah, 033 is an octal number. It's 0x1B in hex (\x1B in literals).
27 makes much more sense now.
Topic archived. No new replies allowed.