我正在为我正在开发的一种语言构建一个简单的解释器,但是我如何cout 一个词后并用“”四舍五入的东西,如下所示:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main( int argc, char* argv[] )
{
if(argc != 2)
{
cout << "Error syntax is incorrect!\nSyntax: " << argv[ 0 ] << " <file>\n";
return 0;
}
ifstream file(argv[ 1 ]);
if (!file.good()) {
cout << "File " << argv[1] << " does not exist.\n";
return 0;
}
string linha;
while(!file.eof())
{
getline(file, linha);
if(linha == "print")
{
cout << text after print;
}
}
return 0;
}
以及如何在打印文本时删除“”。这是文件示例:
print "Hello, World"
在答案中间阅读我的帖子!
谢谢
最佳答案
我希望这个简单的例子能有所帮助。
std::string code = " print \" hi \" ";
std::string::size_type beg = code.find("\"");
std::string::size_type end = code.find("\"", beg+1);
// end-beg-1 = the length of the string between ""
std::cout << code.substr(beg+1, end-beg-1);
此代码找到 的第一次出现。
然后在第一个 之后找到它的下一个出现。最后,它在 之间提取所需的字符串"
并打印出来。
关于c++ - 在 C++ 中读一个词后读一些东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1215688/