我有一个关于从文件中获取信息时如何包含字符串字面值的问题。让我展示一下我的代码以便更好地理解:
Program.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())
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
。有没有办法将其变成实际的字符文字,这样打印时就会打印出一个新行呢?(我也希望引号也能这样处理。)
#define toStr(x) #x
? - amanuel2Program.b
,并使用#include "Program.b"
语句来初始化一个std::string
变量。 - πάντα ῥεῖunescape(file_contents);
,使用那个被接受的特定答案中的unescape
函数不起作用。 - amanuel2cout << unescape(file_contents) << endl;
时似乎可以工作。 - amanuel2