私のポストヒストリーに行くなら、私が取り組んでいる言語のためのインタープリタを開発しようとしていることがわかります。 size_tを2つの異なるコードを使用して使いたいが、それらはすべて返されない。何も私を返さないhttp://stackoverflow.com/questions/1215688/read-something-after-a-word-in-c問題についてsize_t
私はそれをテストしていたファイルを使用しようとすると:ここ
は私がしようとしていたの投稿です。print "This is a print function that i'm trying to develop in my language"
しかし、これはPythonで印刷、どのようなユーザタイプの引用符に(」「)のようなものであることを覚えておいてください:ここでは、サンプルファイル(私は私の言語で開発しようとしてるだけ印刷機能)があります誰もが引用符に入れられたものを選ぶことができることを忘れないでください。単純な魔法のようなものを置いたり、引用符の中にあるものを読み込んで、すべてに印刷したりしてください。しかし、ここでこれを行うには、2つのテスト・コードであるが、それらのすべては、私には何も返しません。
#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
おかげで、 ネイサンPaulinoカンポス
ファイルの拡張子を確認することは本当に良い考えではありません... – Zifre
test.trには何がありますか?あなたのコード(最後の例ではatleast)は、入力がprint(空白や他の空想的なものはありません)という行を含んでいれば、ここでうまくいきます。デバッガでコードをステップ実行すると非常に便利です。 – nos