如果你进入我的帖子历史,你会发现我正在尝试为我正在研究的语言开发一名翻译。我想使用两个不同的代码来使用 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;
}
以下是控制台中打印的内容:
ubuntu@ubuntu-laptop:~/Desktop/Tree$ ./tree test.tr
ubuntu@ubuntu-laptop:~/Desktop/Tree$
像我说的那样,它什么都没打印出来。
请参阅D.I.C中的帖子:http://www.dreamincode.net/forums/showtopic118026.htm
谢谢, Nathan Paulino Campos
答案 0 :(得分:5)
你的问题就在这一行
if (linha == "print")
假设刚刚读入的整行是“打印”,而不是带有打印的行STARTS。
另外,为什么要对.tr扩展名使用3个单独的检查,而只是检查“.tr”的文件名结尾? (在检查子字符串之前,你还应该检查argv [1]是否足够长了......)
答案 1 :(得分:2)
getline(file, linha)
会从文件中读取整行,因此linha
永远不会等于print
。