打印字符串,不含转义字符 [英] Print string without escaping characters
问题描述
此代码:
void Controller :: write(const std :: string& str){
std :: cout<< 写作:[< str<< ] 登上。 << std :: endl;
s.write(str);
需要一个字符串
串行链接到微控制器。 (这工作正常)。但是,当我尝试推这样: write(ats203?\r)
,控制台输出如下:
] to board.ts203?
因此写作:[a
std :: cout
后遇到 \r
在字符串中。
当遇到转义字符时,如何将字符串打印成一堆字符而不是控制台输出?
编辑:澄清。
我希望我的字符串 ats203?\r
。这是因为使用略微修改的 AT命令控制微控制器,并且不接受该命令,直到它读取 \r
。当字符串被推送到微控制器时,它读取 \r
作为回车,并对 \ r
。
我想要的是显示我已经构造的字符串。根据运行时设置的标志,在其他地方有一些其他命令被创建。所以我最终看起来像 ate1 \ratv0\rat + OSA = 7 \rat + OSX = 255 \r
的字符串。
我想看看我的构造和我正在推动什么。我不能这样做,因为 std :: cout
读取回车,作用于它,并且纠正我的控制台输出。
编辑:再次澄清:
我想要 std :: cout< 写作:[< str<< ] 登上。 << std :: endl;
生成(当 str
是 ats203?\r
) :
写作:[ats203?\r]
解决方案
:: write(const std :: string& command_string){
std :: string display_string = std :: regex_replace(command_string,std :: regex((\r)) \\\r);
std :: cout<< 写作:[< display_string<< ] to board.\\\
;
s.write(command_string);
那么
write(ats203?\r)
将逐字打印出ats203?\r
pre>
This code:
void Controller::write(const std::string& str) { std::cout << "Writing: [" << str << "] to board." << std::endl; s.write(str);
takes a
string
and pushes it over a serial link to a microcontroller. (This works fine). However, when I try to push a like this:write("ats203?\r")
, the console output looks like this:] to board.ts203?
So the
Writing: [a
gets overwritten by the] to board.
afterstd::cout
encounters the\r
in the string.How can I print the string as a bunch of characters rather than mangling console output when an escape character is encountered?
EDIT: To clarify.
I want my string to be
ats203?\r
. This is because the microcontroller is controlled using a slightly modified version of an AT command, and doesn't accept the command until it reads a\r
. When the string is pushed to the microcontroller, it reads the\r
as a carriage return, and acts on things it read prior to the\r
.What I want is to display the string that I've constructed. There is a bunch of other commands elsewhere that get created depending on flags that are set at runtime. So I end up with strings that look something like
ate1\ratv0\rat+OSA=7\rat+OSX=255\r
.I would like to see what I've constructed and what I'm pushing. I can't do this, because
std::cout
reads the carriage return, acts on it, and mangles my console output.EDIT: Clarification, again: I want
std::cout << "Writing: [" << str << "] to board." << std::endl;
to produce (whenstr
isats203?\r
):Writing: [ats203?\r] to board.
解决方案void Controller::write(const std::string& command_string) { std::string display_string = std::regex_replace(command_string,std::regex("(\r)"),"\\r"); std::cout << "Writing: [" << display_string << "] to board.\n"; s.write(command_string);
So then
write("ats203?\r")
will literally print outats203?\r
这篇关于打印字符串,不含转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!