如何让编译器识别这些字符
How would i get the compiler to recognize these characters
>我有一个文件,我想打印它的内容,但编译器无法识别以下内容:"/ . \ ____ \ \ _ -." "//__ -/__\_\_\" "\/\ _ \ . /___///" "//_/\ \ _\ \ - __ " "_ \ __/-/\ " " \ -.\ \ \ /__/ 。" "\ \ _\ \ -. \ _ " "- \ ___\_\ '__\_\ " ". /___//////_"
这是我的代码:
int main()
{
MenuText text;
string test = "Champion";
ofstream output("File.txt");
text.save(output);
fstream output ("File.txt");
text.load("File.txt");//This is an error.
text.print();
MenuText::MenuText()
{
mText = "Default";
}
MenuText :: MenuText(string text)
{
mText = text;
}
void MenuText::print()
{
cout<< "Story= " << mText<< endl;
cout<< endl;
}
void MenuText::save(ofstream& outFile)
{
outFile<< "/ . ____ \ \ _ -. "
//"/ /__ - /_______\__\__ "
"__ / __ . /_______//__//"
"__//__/ _ - ________ "
"___ ___ ______ _____/ - / "
"__ \ -. \ ___ /_____/ ."
" __ \ -. \ ___ "
"- ________\__ `___\_____ "
". /_______//__/ /___//_____/ "<< mText<< endl;
cout<< endl;
outFile<< endl;
}
void MenuText::load(ifstream& inFile)
{
string garbage;
inFile>> garbage >> mText;
}
将的发生转义为
\
。你想要其中两个的地方,你需要逃离两个 - \\
.
另请注意,第二行被注释掉了:
//"/ /__ - /_______\__\__ "
怎么样:
"/ . \ \____ \\ \\ _ -. "
"/ /__ - \/\_______\\__\\__\ \"
"__\ /\ __ \ . \/_______//__//"
"__/\/__/ \ \ \_\ \ - ________ "
"___ ___ ______ \ \_____/ - /\ "
"__ \\ -.\ \\ ___\ \/_____/ ."
"\ \ \__\ \\ \-. \\ __\_ "
"- \ _\_______\\__\ `\___\\_____\ "
". \/_______//__/ /___//_____/ ";
<</div>
div class="answers">是文本字符串中的转义字符。如果要表示反斜杠,则需要为每次出现应用两次:
=> \
\ => \\
文本字符串中的"
字符也需要转义"
。
将任何<any_symbol>
对视为控制字符,例如n
是新行,t
是制表符。因此,每次使用反斜杠时,编译器都会尝试将其解释为控制字符,并将下一个符号解释为控制字符。
为避免这种情况,您必须使用另一个反斜杠转义每个反斜杠,因此每次要使用 时,都需要将其替换为
\
。当然,如果你想使用多个反斜杠,你需要转义每一个反斜杠。
据说"不识别"的意思类似于"替换某些字符",或者它解释了非法转义序列:反斜杠是某些特殊字符的前缀。您既不需要将其转义为
\
,或者,对于 C++ 2011,您可以使用原始字符串。例如,"\"
abd R"()"
都应该生成一个反斜杠。
- c++为什么ifstream get()函数返回无法识别的特殊字符
- 字符串数组C++中的字符识别不正确
- 识别数字的字符
- 从文件阅读时未识别的字符
- 字符串中的重复字符是否可以在 O(n) 中识别和量化
- 识别C 程序中的空间字符
- 您如何使用字符串或字符在C 中识别名称识别变量
- C++函数重载无法识别字符
- 如何让编译器识别这些字符
- 试图将字符串作为类的参数传递,但在C++中它被识别为字符数组
- 我可以't识别if语句中的endl字符
- 用于识别回文字符数组的递归函数
- 无法识别以空结尾的字符
- 如何简单地使用无法识别的字符
- 标记字符串,识别字符时出错
- 在if语句中识别为9的8个字符
- 使字符数组在输入时识别空格
- Python/ c++ /Java:是否有一个库可以识别字符并告诉我它们在图像中的位置
- 识别图像上字符的起始和结束XY向量
- std::string 无法识别大于 127 的字符