-1

如果您查看我的帖子历史记录,您会看到我正在尝试为我正在研究的语言开发解释器。我想使用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 中的 print,用户在引号中输入的内容(“”)是必须打印给所有人的,记住用户可以选择引号中的内容,然后不要输入类似 a简单的 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;
}

这是控制台中打印的内容:

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 扩展名使用 3 次单独检查,而不是只检查“.tr”的文件名末尾?(在检查子字符串之前,您还应该检查 argv[1] 是否足够长......)

于 2009-08-02T01:03:20.993 回答
2

getline(file, linha)将从文件中读取整行,因此linha永远不会等于print.

于 2009-08-02T01:02:19.230 回答