如何测试一行是否为空?

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

How to test if a line is empty?

问题

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

这是我的代码:

ifstream fichier("upload.txt", ios::binary);
string ligne;
while (getline(fichier, ligne))
{
    cout << ligne << endl;
    if (ligne == "")
        cout << "line below is empty" << endl;
}

但是,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:

ifstream fichier(&quot;upload.txt&quot;,ios::binary);
string ligne;
while(getline(fichier,ligne))
{
    cout&lt;&lt;ligne&lt;&lt;endl;
    if(ligne==&quot;&quot;)
         cout&lt;&lt;&quot;line below is empty&quot;&lt;&lt;endl;         
}

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将不会被丢弃。因此,你需要进行如下检查:

ifstream fichier("upload.txt", ios::binary);
string ligne;
while (getline(fichier, ligne)) {
	if (ligne.empty() || ligne == "\r") {
		cout << "line is empty" << endl;
	} else {
		cout << ligne << endl;
	}
}

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

ifstream fichier("upload.txt");
string ligne;
while (getline(fichier, ligne)) {
	if (ligne.empty()) {
		cout << "line is empty" << endl;
	} else {
		cout << ligne << endl;
	}
}
英文:

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:

ifstream fichier(&quot;upload.txt&quot;, ios::binary);
string ligne;
while (getline(fichier, ligne)) {
	if (ligne.empty() || ligne == &quot;\r&quot;) {
		cout &lt;&lt; &quot;line is empty&quot; &lt;&lt; endl;
	} else {
		cout &lt;&lt; ligne &lt;&lt; endl;
	}
}

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

ifstream fichier(&quot;upload.txt&quot;);
string ligne;
while (getline(fichier, ligne)) {
	if (ligne.empty()) {
		cout &lt;&lt; &quot;line is empty&quot; &lt;&lt; endl;
	} else {
		cout &lt;&lt; ligne &lt;&lt; endl;
	}
}

答案2

得分: 0

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

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

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

    ifstream fichier(&quot;upload.txt&quot;, std::ifstream::in ); //File opened for reading.
    string ligne;
    //Always Check if file is properly opened.
    if (fichier.is_open() ) 
    {
       while(getline(fichier,ligne))
       {
          cout&lt;&lt;ligne&lt;&lt;endl;
           if(ligne.empty() || str.find_first_not_of(&#39; &#39;) == std::string::npos)
            cout&lt;&lt;&quot;line below is empty&quot;&lt;&lt;endl;         
       }
       fichier.close();
    
    }
    else 
    {
     // show message:
      std::cout &lt;&lt; &quot;Error opening file&quot;;
    }
    

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:

确定