如何测试一行是否为空?

问题描述 投票:0回答:1

我实际上是想了解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条件似乎不起作用:((

c++ string ifstream getline
1个回答
0
投票

尝试这个:

    ifstream fichier("upload.txt", 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<<ligne<<endl;
           if(ligne.isempty())
            cout<<"line below is empty"<<endl;         
       }

    }
   else 
   {
    // show message:
     std::cout << "Error opening file";
   }
© www.soinside.com 2019 - 2024. All rights reserved.