Nat*_*pos -1 c++ size-t size-type
如果你进入我的帖子历史,你会发现我正在尝试为我正在研究的语言开发一个翻译.我想使用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"
Run Code Online (Sandbox Code Playgroud)
但请记住,这就像在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;
}
Run Code Online (Sandbox Code Playgroud)
第二:
#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;
}
Run Code Online (Sandbox Code Playgroud)
以下是控制台中打印的内容:
ubuntu@ubuntu-laptop:~/Desktop/Tree$ ./tree test.tr
ubuntu@ubuntu-laptop:~/Desktop/Tree$
Run Code Online (Sandbox Code Playgroud)
就像我说的,它没有打印任何东西. 请参阅DIC中的帖子:http://www.dreamincode.net/forums/showtopic118026.htm
谢谢Nathan Paulino Campos
你的问题就在于此
if (linha == "print")
Run Code Online (Sandbox Code Playgroud)
假设刚刚读入的整行是"打印",而不是带有打印的行STARTS.
另外,为什么要对.tr扩展名使用3个单独的检查,而只是检查".tr"文件名的结尾?(在检查子字符串之前,您还应该检查argv [1]是否足够长...)