如何测试一行是否为空?

huangapple go评论139阅读模式
英文:

How to test if a line is empty?

问题

我实际上正在尝试理解getline()函数是如何工作的!但我遇到了测试一行是否为空的困难!

这是我的代码:

  1. ifstream fichier("upload.txt", ios::binary);
  2. string ligne;
  3. while (getline(fichier, ligne))
  4. {
  5. cout << ligne << endl;
  6. if (ligne == "")
  7. cout << "line below is empty" << endl;
  8. }

但是,if条件似乎不起作用:((

英文:

I'm actually trying to understand how does the getline() function works! But I'm facing difficulties to test if a line is empty or not!

Here is my code:

  1. ifstream fichier(&quot;upload.txt&quot;,ios::binary);
  2. string ligne;
  3. while(getline(fichier,ligne))
  4. {
  5. cout&lt;&lt;ligne&lt;&lt;endl;
  6. if(ligne==&quot;&quot;)
  7. cout&lt;&lt;&quot;line below is empty&quot;&lt;&lt;endl;
  8. }

But, the if-condition seems to be not working :((

答案1

得分: 2

在Windows上,换行通常是CRLF(0x0D 0x0A)。std::getline()会读取直到遇到LF,并将其从返回的std::string中丢弃。如果std::ifstream以文本模式(默认模式)打开,平台的换行会被规范化为LF,并且前导的CR也会被丢弃。但是,如果以二进制模式打开,CR将不会被丢弃。因此,你需要进行如下检查:

  1. ifstream fichier("upload.txt", ios::binary);
  2. string ligne;
  3. while (getline(fichier, ligne)) {
  4. if (ligne.empty() || ligne == "\r") {
  5. cout << "line is empty" << endl;
  6. } else {
  7. cout << ligne << endl;
  8. }
  9. }

否则,不要在文本文件中使用二进制模式:

  1. ifstream fichier("upload.txt");
  2. string ligne;
  3. while (getline(fichier, ligne)) {
  4. if (ligne.empty()) {
  5. cout << "line is empty" << endl;
  6. } else {
  7. cout << ligne << endl;
  8. }
  9. }
英文:

On Windows, a line break is normally CRLF (0x0D 0x0A). std::getline() will read until it encounters the LF and discard it from the returned std::string. If the std::ifstream is opened in text mode (the default mode), platform line breaks are normalized to LF and a leading CR will also be discarded. But, if opened in binary mode, the CR will not be discarded. So you will have to check for that:

  1. ifstream fichier(&quot;upload.txt&quot;, ios::binary);
  2. string ligne;
  3. while (getline(fichier, ligne)) {
  4. if (ligne.empty() || ligne == &quot;\r&quot;) {
  5. cout &lt;&lt; &quot;line is empty&quot; &lt;&lt; endl;
  6. } else {
  7. cout &lt;&lt; ligne &lt;&lt; endl;
  8. }
  9. }

Otherwise, don't use binary mode for text files:

  1. ifstream fichier(&quot;upload.txt&quot;);
  2. string ligne;
  3. while (getline(fichier, ligne)) {
  4. if (ligne.empty()) {
  5. cout &lt;&lt; &quot;line is empty&quot; &lt;&lt; endl;
  6. } else {
  7. cout &lt;&lt; ligne &lt;&lt; endl;
  8. }
  9. }

答案2

得分: 0

你的问题是以二进制方式打开文件。除此之外,你还应该执行以下几步:

  1. 检查文件是否正确打开。
  2. 与 empty() 进行比较。
  3. 检查字符串是否只包含空格,如果是,则视为空。
  4. 关闭文件。
  1. ifstream fichier("upload.txt", std::ifstream::in); // 以读取方式打开文件。
  2. string ligne;
  3. // 总是检查文件是否正确打开。
  4. if (fichier.is_open())
  5. {
  6. while (getline(fichier, ligne))
  7. {
  8. cout << ligne << endl;
  9. if (ligne.empty() || str.find_first_not_of(' ') == std::string::npos)
  10. cout << "下面的行为空" << endl;
  11. }
  12. fichier.close();
  13. }
  14. else
  15. {
  16. // 显示消息:
  17. std::cout << "打开文件出错";
  18. }
英文:

Your problem is opening file in binary. Other than that, you should do few things :

  1. Check if file is properly opened

  2. Compare against empty()

  3. Check if string only contains space , then it's considered empty.

  4. Close file

    1. ifstream fichier(&quot;upload.txt&quot;, std::ifstream::in ); //File opened for reading.
    2. string ligne;
    3. //Always Check if file is properly opened.
    4. if (fichier.is_open() )
    5. {
    6. while(getline(fichier,ligne))
    7. {
    8. cout&lt;&lt;ligne&lt;&lt;endl;
    9. if(ligne.empty() || str.find_first_not_of(&#39; &#39;) == std::string::npos)
    10. cout&lt;&lt;&quot;line below is empty&quot;&lt;&lt;endl;
    11. }
    12. fichier.close();
    13. }
    14. else
    15. {
    16. // show message:
    17. std::cout &lt;&lt; &quot;Error opening file&quot;;
    18. }

huangapple
  • 本文由 发表于 2020年1月4日 00:46:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定