【问题标题】:How can I check a string to see if a carriage return exists in C++?如何检查字符串以查看 C++ 中是否存在回车?
【发布时间】:2013-06-28 23:47:03
【问题描述】:
vector<string> wordstocheck;
in.open("readin.txt");

string line;
string word = "";
int linecount = 0;


while (getline(in, line))
{
    //cout << line << endl;
    for (int i = 0; i < line.size(); i++)
    {
        if(isalpha(line[i]))
        {
            word.push_back(tolower(line[i]));
        }
        else if (line[i] == ' ' || ispunct(line[i]) || line[i] == '\n')
        {
            wordstocheck.push_back(word);
            word = "";
        }

    }

linecount++;
}
for (int i = 0; i < wordstocheck.size(); i++)
    {
        cout << wordstocheck[i] << endl;
    }
system("pause");
}

上面的代码从 .txt 文件中读取以下内容:

 If debugging is the
 process of removing bugs.
 Then programming must be the
 process of putting them in.

我试图让程序识别每个单词,并将单个单词保存到一个向量中,然后打印出该单词向量。除了第一行和第三行的两个 'the' 之外,它的表现都很好。

Output:
if
debugging
is
theprocess
of
removing
bugs
then
programming
must
be
theprocess
of
putting
them
in
Press any key to continue . . .

它并没有像我希望的那样拆分“过程”。

【问题讨论】:

    标签: c++ split return line


    【解决方案1】:

    getline 不会读取换行符。但是,在这种情况下,解决此问题相对简单。

    如果您当前有 linecount++;,请在其前面添加以下行:

       if (word != "")
       {
            wordstocheck.push_back(word);
            word = "";
       }
    

    您可能希望在将word 推送到wordstocheck 的第一个位置使用相同的if (word != ""),因为如果文本有“A Word”,您将添加单词“A”,后跟一个空词作为秒空间触发要添加到列表中的词。

    作为替代方案,您可以摆脱 getline,而只需使用 int ch = in.get() 从输入中一次读取一个字符。然后不是在while()... 中计算行数,而是在循环中使用ch 而不是line[i] al,然后在else if 部分中添加第二个if,它检查换行符并计算行数。这可能会使代码更短。

    【讨论】:

    • 非常感谢!一次读取一个字符效果很好!我现在唯一的问题是我的“while (int ch = in.get())”无限循环。到达文件末尾后如何告诉它退出?
    【解决方案2】:

    我认为问题在于您希望换行符包含在来自getline() 的结果中,但事实并非如此。看起来如果您采用该块中已有的两行:

    wordstocheck.push_back(word);
    word = "";
    

    并将它们添加到行旁边:

    linecount++;
    

    那么它应该可以按您的预期工作。

    【讨论】:

    • 非常感谢!你的也解决了我的问题。我能够通过一次读取一个字符来获得短代码。
    【解决方案3】:

    如果你想一次读一个单词,为什么首先使用std::getline

    // read the words into a vector of strings:
    std::vector<std::string> words{std::istream_iterator<std::string(in),       
                               std::istream_iterator<std::string()};
    

    您可以使用std::for_eachstd::transform 将所有内容转换为小写,最后使用for (auto const &amp;w : words) std::cout &lt;&lt; w &lt;&lt; "\n"; 打印出来

    【讨论】:

      【解决方案4】:

      到目前为止,我知道,getline 读取整行并且不识别回车符。我知道的唯一方法是读取文件,逐个字符地读取它。 这是一个给出正确结果的示例:

      #include <iostream>     // std::cin, std::cout
      #include <fstream>      // std::ifstream
      
      int main ()
      {
          char str[256];
          int line = 1;
          int charcount = 0;
      
          std::cout << "Enter the name of an existing text file: ";
          std::cin.get (str,256);
      
          std::ifstream is(str);
          if (!is)
          {
              std::cerr << "Error opening file!" << std::endl;
              return -1;
          }
      
          char c;
          while ((c = is.get()) && is.good()) // loop while extraction from file if possible
          {
              if (c == 10 || c == 13 || c == 32) // if it is a line break or carriage return or space
              {
                  std::cout << std::endl;
                  line++;
              }
              else // everything else
              {
                  std::cout << c;
                  charcount++;
              }
          }
          is.close();
          std::cout << std::endl;                // close file
          std::cout << line << " lines" << std::endl;
          std::cout << charcount << " chars" << std::endl;
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-02
        相关资源
        最近更新 更多