-1

私の投稿履歴を見ると、私が取り組んでいる言語の通訳者を開発しようとしていることがわかります。2つの異なるコード を使用して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での印刷のようなものであり、ユーザーがquotes( "")に入力するものはすべてに印刷する必要があることを忘れないでください。ユーザーは引用符に入れるものを選択できるので、簡単なカウト、引用符の中にあるものを読んで、それをすべてに印刷する何かを投稿してください。しかし、これを行うための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;
}

そして、これがコンソールに印刷されるものです:

ubuntu@ubuntu-laptop:~/Desktop/Tree$ ./tree test.tr
ubuntu@ubuntu-laptop:~/Desktop/Tree$

私が言ったように、それは私に何も印刷しません。 DICの私の投稿を参照してください:http ://www.dreamincode.net/forums/showtopic118026.htm

ありがとう、ネイサン・パウリーノ・カンポス

4

2 に答える 2

5

あなたの問題はラインです

if (linha == "print")

これは、読み込まれたばかりの行全体が「印刷」であると想定しており、行が印刷で始まるとは限りません。

また、ファイル名の末尾の「.tr」をチェックするのではなく、.tr拡張子に3つの別々のチェックを使用するのはなぜですか?(サブストリングをチェックする前に、argv [1]が十分に長いこともチェックする必要があります...)

于 2009-08-02T01:03:20.993 に答える
2

getline(file, linha)ファイルから1行全体を読み取るため、 。linhaと等しくなることはありませんprint

于 2009-08-02T01:02:19.230 に答える