gpt4 book ai didi

c++ - 如何包含字 rune 字?

转载 作者:可可西里 更新时间:2023-11-01 17:44:31 25 4
gpt4 key购买 nike

我有一个关于从文件中获取信息时如何包含字符串文字的问题。让我展示我的代码以便更好地理解:

程序.b:

print \"Hello World\n\"; print \"Commo Estas :)\n\"; print \"Bonjour\";print \"Something\"; return 0;

ma​​in.cpp(我已将实际文件最小化为这个问题所需的文件):

int main()
{
std::string file_contents;
std::fstream file;
file.open("Program.b");
std::ifstream file_read;
file_read.open("Program.b");

if(file_read.is_open())
while(getline(file_read,file_contents));

cout << file_contents << endl;

}

所以现在当我打印 file_contents 时,我得到:

print \"Hello World\n\"; print \"Commo Estas :)\n\"; print \"Bonjour\";print \"Something\"; return 0;

您可以看到它包含 \n 。有没有办法让它成为一个实际的字 rune 字,所以打印它实际​​上会打印一个新行? (我想要同样的引号。)

最佳答案

尝试这样的事情:

程序.b

R"inp(print "Hello World\n"; print "Commo Estas :)\n"; print "Bonjour";print "Something"; return 0;)inp"

main.cpp

int main() {
std::string file contents =
#include "Program.b"
;
std::cout << file_contents << std::endl;

}

您还可以更改 Program.b 以使其更具可读性:

R"inp(
print "Hello World\n";
print "Commo Estas :)\n";
print "Bonjour";
print "Something";
return 0;
)inp"

运行时变体应该很简单:

程序.b

print "Hello World\n"; 
print "Commo Estas :)\n";
print "Bonjour";
print "Something";
return 0;

main.cpp

int main()
{
std::string file_contents;
std::fstream file;
file.open("Program.b");
std::ifstream file_read;
file_read.open("Program.b");

if(file_read.is_open()) {
std::string line;
while(getline(file_read,line)) {
file_contents += line + `\n`;
}
}

cout << file_contents << endl;

}

关于c++ - 如何包含字 rune 字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40113814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com