Cannot output ansi colour codes with fstream

I'm having trouble with the outputting of strings containing ansi codes.

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
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

string TEST;


void readFile()
{
	fstream FILE;
	
	FILE.open("filetoread.txt");
        // Contents of file are exactly: \e[1;32m this is a test

	getline(FILE, TEST);

	FILE.close();
	FILE.clear();
}



int main()
{
	system("CLS");


	readFile();

	printf("%s\n", TEST.c_str());

	return 0;
}


The string outputted is still: "\e[1;32m this is a test"
Last edited on
It doesn't even compile.
1
2
3
 In function 'int main()':
30:17: error: 'TEST' was not declared in this scope
 


> Contents of file are exactly: \e[1;32m this is a test
Backslash escapes are only interpreted in things like source code and say the command prompt.

If you're reading from a file, every character stands for itself.
With the exception (for your platform) that \r\n combinations are transformed to \n on input.

If you want an actual escape character, then you need to replace the two characters '\e' with an actual escape character. How you do this depends on your text editor.
Ah yes I see why it wouldn't compile for you, some how the TEST string was put inside the function itself (no idea how it got in there).

It should be:
1
2
3
using namespace std;

string TEST;


And it will compile, sorry for that.
I don't know if I understand correctly. Show us what happens when you type

od -t x1 filetoread.txt
@Duthomhas

This gives me a compile error:
1
2
3
4
FILE.open(od -t x1 "filetoread.txt");
// compile error:
// 'od' was not declared in this scope.
// 't' was not declared in this scope. 
gurgle...

https://www.geeksforgeeks.org/od-command-linux-example/
so no, stuffing that into c++ won't work well. well, could do system() of it... but no.. just type it in the console :)

I am not sure what you are trying to do. But its possible its not supported directly; you may have to read the code from the file and parse it, send the correct code to the console, then push the file data through in that colored setting. Taken as a string, the codes are just text... you have to get the console to accept it as a command, and it may not do that if its with other text or something. I admit to not playing with these much, so I could be wrong.

maybe something in here will help too:
https://stackoverflow.com/questions/9158150/colored-output-in-c

see comments too, eg
It will work in Terminal, but not in the Xcode console window
Last edited on
@joinnin
@Duthomhas

The result of typing the command is:

$ od -t x1 "filetoread.txt"
0000000 5c 65 5b 31 3b 33 32 6d 44 49 44 20 52 45 41 44
0000020 20 46 49 4c 45 5c 6e 0d 0a
0000031
---------------------------------------------------------------------------------
Last edited on
ok. That is the hex dump of the file, but I can't say what Duthomas wanted to see there. But that ^^ is what he wanted, I guess.
See the picture.
https://imgur.com/a/MfcpJm4

Your text file needs to contain an actual escape character (1b), not the two character pair \e (5c 65)
@salem c

how do I obtain the escape char (1B)?
Depends on what editor you're using to edit your text files.

https://duckduckgo.com/?q=notepad%2B%2B+insert+special+characters
Just change notepad++ to the name of your editor.

Most competent text editors, especially ones aimed at programmers should be able to do this one way or another.
@salem c

I managed to get it working!

I used https://hexed.it/ to get the special character and then put that into the txt file.

if anybody could explain to me why using the '\e' in the text file doesn't work, that would be awesome, thanks again every one for helping!
In c/c++, '\e' isn't a recognised escape sequence. To get 'escape' you can use '\x1b' or '\033'

See https://en.cppreference.com/w/cpp/language/escape for a list
Last edited on
> if anybody could explain to me why using the '\e' in the text file doesn't work, that would be awesome,
Because backslash escapes require some kind of parser, say
- your C++ compiler
- your shell

If you were reading a text file of path names, like
c:\path\to\some\file.txt
c:\here\is\another\file.txt


Then backslash interpretation from doing things like getline(FILE, TEST); would become such an annoying PITA really fast that you'd be here asking "how do I turn off this *!$?! backslash processing!?".

Topic archived. No new replies allowed.