如何让编译器识别这些字符

How would i get the compiler to recognize these characters

本文关键字:字符 识别 编译器      更新时间:2023-10-16

>我有一个文件,我想打印它的内容,但编译器无法识别以下内容:"/ . \ ____ \ \ _ -." "//__ -/__\_\_\" "\/\ _ \ . /___///" "//_/\ \ _\ \ - __ " "_ \ __/-/\ " " \ -.\ \ \ /__/ 。" "\ \ _\ \ -. \ _ " "- \ ___\_\ '__\_\ " ". /___//////_"

这是我的代码:

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"()" 都应该生成一个反斜杠。