【问题标题】:Read input until a specific string is found C++读取输入直到找到特定字符串 C++
【发布时间】:2016-10-06 19:06:23
【问题描述】:

我目前正在使用此代码接收标准输入并将其放入字符串中。然后使用分隔符标记化 #

std::string input;
std::getline(std::cin,input, '\0');
std::string delimiter = "#";
StringTokenizer strtok(input,delimiter);

这使得 getline 读取直到找到空字符(我认为)并将整个字符串传递给输入。当我运行 cmd 并编写此代码时,这在 windows 中工作正常:

myprogram.exe 0 <testme.txt

我得到了我期望的输出。我遇到的问题是当我尝试在 linux 终端中运行类似的命令时(a.out 是我的程序)

./a.out < testme.txt 

在我按下 ctrl-z 之前它不会停止寻找键盘输入。当我在编写程序的 IDE Code::blocks 中运行时也是这种情况。 Code::blocks 和 linux 都使用 g++ 编译,没有错误或警告。我认为我的问题在于等待空字符。使用空字符的原因是因为我的输入是多行的,当我刚刚使用 getline(std::cin,input) 时,只读取了第一行。

示例 testme.txt

 A B C D E F #
 A -> Bab #
 B -> C #
 ##

我正在处理我的代码的示例

std::string data;
std::getline(std::cin,data, '\0');
std::string delimiter = "#";
StringTokenizer strtok(data,delimiter);
// places first token in tk1
std::string t1      = strtok.nextToken();
std::string tk1(t1);
// places next token in tk2
std::string t2      = strtok.nextToken();
std::string tk2(t2);
if(checkstring(tk2,"a") > 0) // this function checks the amount of "a"s in the token. I only care if there is at least 1 "a" so that is what the if statements are for.
    {
      a = a + 1;
    }
// do this if statement for every letter
// example: token = A -> abbbababababa then will print out a = 1 and b = 1

如果通过我的程序 testme.txt 应该让我的程序输出这个:

 A B C D E F
 a= 1
 b= 1

我的问题

我的每个 testme.txt 文件都以两个字符 ## 结尾。有没有一种方法可以在到达此字符串(?)时停止读取输入,而不是等待空字符?

【问题讨论】:

  • strtok() 是最糟糕的选择。
  • 添加与第一个不同字符的第二个分隔符?
  • 为什么不传递文件名而只读取文件?!
  • @deW1 我需要接受标准输入,所以我不知道 txt 的名称是什么。它必须适用于任何标准输入。
  • 你能更准确地说出你想要什么吗?

标签: c++ linux string


【解决方案1】:
do{
//do stuff
if (yourString == "##")
    break;
}while(yourString != NULL);

有时它比看起来容易

编辑
如果你删除它们,你所有的'#'都是无用的:

std::vector<std::string> tokens; // Storing all tokens
std::string line;
while (std::getline(file, line))
    tokens.push_back(line);

for (std::string s : tokens) //Gets all tokens one by one
    for (char c : s)// Gets all char in each token
        if (islower(c))// Checks if thats a lowercase letter
            std::cout << c << std::endl;

如果你想保留'#':

while (std::getline(file, line))
    if (!line.find("#"));
        tokens.push_back(line);

如果您使用 std::cin 而不是文件:

std::string input, line;
std::cin >> input;
std::stringstream sinput(input);

while (std::getline(sinput, line, '#'))
    tokens.push_back(line);

【讨论】:

  • 简短地告诉我你的程序是做什么的
  • 我的程序读入标准输入并将其放入字符串(输入),然后使用 strtok 通过分隔符 # 标记该字符串。然后它调用函数 nexttoken() 并将令牌放入字符串中。然后它检查 tk1 之后的每个标记,如果该标记包含小写字母并输出 ex:(如果 tk2 包含“a”,那么它将输出 a = 1 等)每个输入文件都以 ## 结尾。
  • 我认为唯一需要改变的是我在输入中阅读的方式。 getline(std::string,input,'\0') 似乎永远不会停止寻找输入,因为它找不到 \0。有没有办法让 getline() (或其他函数)在看到 ## 时停止寻找输入?
  • 不是 getline() 本身,但是当 getline() 修改的字符串等于“##”时,您可以停止,就像我的第一个答案一样,getline() 在它读取的文件的结尾
  • 我会写 if(std::cin == "##") 还是我会在那里输入?我也收到此错误“与'operator !='不匹配”
猜你喜欢
  • 1970-01-01
  • 2014-05-09
  • 2011-10-29
  • 1970-01-01
  • 2012-09-04
  • 2018-10-02
  • 2023-03-23
  • 1970-01-01
  • 2014-11-10
相关资源
最近更新 更多