英文:
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("upload.txt",ios::binary);
string ligne;
while(getline(fichier,ligne))
{
cout<<ligne<<endl;
if(ligne=="")
cout<<"line below is empty"<<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("upload.txt", ios::binary);
string ligne;
while (getline(fichier, ligne)) {
if (ligne.empty() || ligne == "\r") {
cout << "line is empty" << endl;
} else {
cout << ligne << endl;
}
}
Otherwise, don't use binary mode for text files:
ifstream fichier("upload.txt");
string ligne;
while (getline(fichier, ligne)) {
if (ligne.empty()) {
cout << "line is empty" << endl;
} else {
cout << ligne << endl;
}
}
答案2
得分: 0
你的问题是以二进制方式打开文件。除此之外,你还应该执行以下几步:
- 检查文件是否正确打开。
- 与 empty() 进行比较。
- 检查字符串是否只包含空格,如果是,则视为空。
- 关闭文件。
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 :
-
Check if file is properly opened
-
Compare against empty()
-
Check if string only contains space , then it's considered empty.
-
Close file
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.empty() || str.find_first_not_of(' ') == std::string::npos) cout<<"line below is empty"<<endl; } fichier.close(); } else { // show message: std::cout << "Error opening file"; }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论