匹配词c++;使用getline()无限运行的程序? 我学习C++,所以容忍我并为任何愚蠢的行为道歉。< /P>
我试图编写一些代码,将名为“command.txt”的文件中每行的第一个单词与“num_行”、“num_单词”或“num_字符”匹配 如果第一行的第一个单词与前面提到的单词不匹配,它将读取下一行。 一旦找到匹配的单词(仅限第一个单词!),它就会打印出匹配的单词 以下是我的全部代码:匹配词c++;使用getline()无限运行的程序? 我学习C++,所以容忍我并为任何愚蠢的行为道歉。< /P>,c++,ifstream,getline,C++,Ifstream,Getline,我试图编写一些代码,将名为“command.txt”的文件中每行的第一个单词与“num_行”、“num_单词”或“num_字符”匹配 如果第一行的第一个单词与前面提到的单词不匹配,它将读取下一行。 一旦找到匹配的单词(仅限第一个单词!),它就会打印出匹配的单词 以下是我的全部代码: #include <iostream> #include <fstream> #include <string> using namespace std; ifstre
#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"
}
它可以正确编译,但当我在终端中运行它时,什么也没有显示;它似乎从未停止加载。
如何修复此问题?如果出现问题,并且到达文件末尾,循环将永远不会停止。如果(!getline(comm_in,line))中断,则应将
getline(comm_in,line)
更改为
,或者更好,将其用作循环的条件
您还必须为每个过程重置命令\u 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;
}
while(getline(comm_-in,line))
{
命令_name=“”;
对于(int i=0;i
除非你真的想在早上痛恨自己(可以这么说),否则你想摆脱使用全局变量的习惯。如果您将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;
}
}
bool是_cmd(std::string const&s){
返回s==“num|u words”| s==“num|u chars”| s==“num|u行”;
}
std::字符串首字母(std::istream&is){
std::字符串行,ret;
if(std::getline(is,line)){
自动启动=行。首先查找(“\t”)的\u而不是\u;
自动结束=行。查找(“\t”,开始)的第一行;
ret=行.substr(开始,结束-开始);
}
返回ret;
}
void get_命令(std::istream&is){
std::string cmd;
while(!(cmd=first_word(is)).empty()
if(is_cmd(cmd)){
std::cout//在文件中查找与“num\u单词”、“num\u字符”或“num\u行”匹配的行的第一个单词
void get_命令()
{
弦线;
字符c;
而(!is_command()){//如果命令名与命令不匹配
//获取要字符串的文件的下一行
if(getline(comm_-in,line),comm_-in.fail()){
//结束阅读
打破
}
//清楚的
命令_name=“”;
//应该获取字符串的第一个单词(不能使用SSTREAM)
对于(int i=0;i
这个问题的关键是您没有清除命令\u name
此外,您还必须添加一个判断是否到达文件末尾
ps:if(getline(comm_-in,line),comm_-in.fail())
等于if(getline(comm_-in,line))
,查看你的while
循环在做什么。什么会导致ti停止?while循环在is_命令返回true时停止,即当命令名=“num_-line”时停止因此,一旦读取command.txt文件的第4行,它就必须停止?这个逻辑是否有问题?是这样吗?一旦退出循环,它应该打印并退出程序。
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;
}
}
// 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;
}