的问题如果你在我的博文历史中,你会看到我正在尝试开发一种我正在使用的语言的解释器。我想使用size_t使用两个不同的代码,但它们都不返回任何内容。关于size_t
这里是我试图帖子:http://stackoverflow.com/questions/1215688/read-something-after-a-word-in-c
当我尝试使用我测试它的文件返回我什么。下面是示例文件(仅打印功能,我试图在我的语言来开发):
print "This is a print function that i'm trying to develop in my language"
但请记住,这就像印刷在Python,什么用户类型为引号(”“)是必须要打印给所有人的,记住用户可以选择放入引号中的内容,然后不要放入类似简单的cout的东西,发布引用内容并将其打印出来的内容。但这里是两个测试代码要做到这一点,但他们都没什么不返回到我:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[])
{
// Error Messages
string extension = argv[ 1 ];
if(argc != 2)
{
cout << "Error syntax is incorrect!\nSyntax: " << argv[ 0 ] << " <file>\n";
return 0;
}
if(extension[extension.length()-3] != '.')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
if(extension[extension.length()-2] != 't')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
if(extension[extension.length()-1] != 'r')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
// End of the error messages
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")
{
size_t idx = linha.find("\""); //find the first quote on the line
while (idx != string::npos) {
size_t idx_end = linha.find("\"",idx+1); //end of quote
string quotes;
quotes.assign(linha,idx,idx_end-idx+1);
// do not print the start and end " strings
cout << "quotes:" << quotes.substr(1,quotes.length()-2) << endl;
//check for another quote on the same line
idx = linha.find("\"",idx_end+1);
}
}
}
return 0;
}
第二:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[])
{
// Error Messages
string extension = argv[ 1 ];
if(argc != 2)
{
cout << "Error syntax is incorrect!\nSyntax: " << argv[ 0 ] << " <file>\n";
return 0;
}
if(extension[extension.length()-3] != '.')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
if(extension[extension.length()-2] != 't')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
if(extension[extension.length()-1] != 'r')
{
cout << "Extension not valid!" << endl;
cout << "Default extension *.tr" << endl;
return 0;
}
// End of the error messages
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")
{
string code = " print \" hi \" ";
size_t beg = code.find("\"");
size_t end = code.find("\"", beg+1);
// end-beg-1 = the length of the string between ""
cout << code.substr(beg+1, end-beg-1);
}
}
return 0;
}
这里是什么是打印到控制台上:
[email protected]:~/Desktop/Tree$ ./tree test.tr
[email protected]:~/Desktop/Tree$
就像我说的,它没有印出任何东西。 请参见我的文章在D.I.C:http://www.dreamincode.net/forums/showtopic118026.htm
感谢, 森保利诺坎波斯
检查文件扩展名真的不是一个好主意... – Zifre 2009-08-02 01:04:11
What's in your test.tr?你的代码(最后一个例子atleast)在这里提供的输入包含一行说print(没有任何空格或任何其他花哨的东西)的工作正常。在调试器中执行代码将非常有帮助。 – nos 2009-08-02 01:10:38