2017-11-11 112 views
1

我正在学习C++,所以请耐心等待,并且事前为任何白痴道歉。匹配使用getline()无限运行的单词C++程序?

我试图写一些代码,将每行上的第一个字匹配到“num_lines”,“num_words”或“num_chars”文件中,名为“command.txt”。

如果第一行的第一个单词与先前提到的单词不匹配,它将读取下一行。 一旦它遇到匹配的单词(仅第一个单词!)它打印出匹配的单词。

这里是我的所有代码:

#include <iostream> 
#include <fstream> 
    #include <string> 

using namespace std; 

ifstream comm_in("commands.txt"); // opens file 
string command_name = "hi"; // stores command from file 


bool is_command() { 
    if (command_name == "num_words" || command_name == "num_chars" || command_name == "num_lines") { 
     return true; 
    } else { 
     return false; 
    } 
} 


// FIND a first word of a line in file THAT MATCHES "num_words", "num_chars" or "num_lines" 
void get_command() { 

    string line; 
    char c; 

    while (!is_command()) { // if command_name does not match a command 

     // GET NEXT LINE OF FILE TO STRING 
     getline(comm_in, line); 

     // SUPPOSED TO GET THE FIRST WORD OF A STRING (CANT USE SSTREAM) 
     for (int i = 0; i < line.size(); i++) { // increment through line 
      c = line[i]; // assign c as index value of line 

      if (c == ' ' || c == '\t') { // if c is a space/tab 
       break; // end for loop 
      } else { 
       command_name += c; // concatenate c to command_name 
      } // if 
     } // for 
    } // while 
    return; 
} 

int main() { 

    get_command(); 
    cout << command_name; // supposed to print "num_lines" 
} 

的command.txt文件的内容:

my bear is happy 
and that it 
great ha 
num_lines sigh 

编译正确,但是当我在终端运行它,什么也不显示;它似乎永远不会停止加载。 我该如何解决这个问题?

+0

看看你的'while'循环做。什么会导致ti停止? – Jay

+0

while循环在is_command返回true时停止,即当command_name ==“num_lines”时,它一旦读取到command.txt文件的第4行就必须停止?这个逻辑有什么问题吗? – Salvatross

+0

这是怎么回事?一旦退出循环,它应该打印并退出程序。 – Jay

回答

0

如果出现问题并且您到达文件末尾,循环将永不停止。您应该将getline(comm_in, line)更改为if(!getline(comm_in, line)) break;,或者更好,将其用作循环的条件。

您还可以重置command_name每一遍:

while(getline(comm_in, line)) 
{ 
    command_name = ""; 
    for(int i = 0; i < line.size(); i++) 
    { 
     c = line[i]; 
     if(c == ' ' || c == '\t') 
      break; 
     else 
      command_name += c; 
    } 
    if(is_command()) 
     break; 
} 
1

除非你真的要恨自己在早上(这么说)你想摆脱使用全局变量的习惯。如果您将get_command分为(至少)两个函数(特别是从包含该行的字符串中获取第一个字),您几乎肯定会发现生活更简单。

我会写代码更是这样的:

bool is_cmd(std::string const &s) { 
    return s == "num_words" || s == "num_chars" || s == "num_lines"; 
} 

std::string first_word(std::istream &is) { 
    std::string line, ret; 

    if (std::getline(is, line)) { 
     auto start = line.find_first_not_of(" \t"); 
     auto end = line.find_first_of(" \t", start); 
     ret = line.substr(start, end - start); 
    } 
    return ret; 
} 

void get_command(std::istream &is) { 
    std::string cmd; 

    while (!(cmd = first_word(is)).empty()) 
     if (is_cmd(cmd)) { 
      std::cout << cmd; 
      break; 
     } 
} 

这仍然是不完美的(例如,形成严重的输入可能仍然导致失败),但至少它是什么我一招d说是更好的方向。

0
// FIND a first word of a line in file THAT MATCHES "num_words", "num_chars" or "num_lines" 
void get_command() 
{ 
    string line; 
    char c; 

    while (!is_command()) { // if command_name does not match a command 

     // GET NEXT LINE OF FILE TO STRING 
     if(getline(comm_in, line),comm_in.fail()){ 
      // end reading 
      break; 
     } 

     //clear 
     command_name = ""; 

     // SUPPOSED TO GET THE FIRST WORD OF A STRING (CANT USE SSTREAM) 
     for (int i = 0; i < line.size(); i++) { // increment through line 
      c = line[i]; // assign c as index value of line 

      if (c == ' ' || c == '\t') { // if c is a space/tab 
       break; // end for loop 
      } else { 
       command_name += c; // concatenate c to command_name 
      } // if 
     } // for 
    } // while 
    return; 
} 

这个问题的关键是你没有明确的command_name

更重要的是,您必须添加一个关于是否达到文件末尾的判断。

PS:if(getline(comm_in, line),comm_in.fail())等于if(getline(comm_in, line))