运行以下代码时,读取的行数会少于实际存在的行数(如果输入文件是main本身,或者其他) 为什么会这样,我怎么能改变这个事实(除了只添加1)?
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
// open text file for input
string file_name;
cout << "please enter file name: ";
cin >> file_name;
// associate the input file stream with a text file
ifstream infile(file_name.c_str());
// error checking for a valid filename
if ( !infile ) {
cerr << "Unable to open file "
<< file_name << " -- quitting!\n";
return( -1 );
}
else cout << "\n";
// some data structures to perform the function
vector<string> lines_of_text;
string textline;
// read in text file, line by line
while (getline( infile, textline, '\n' )) {
// add the new element to the vector
lines_of_text.push_back( textline );
// print the 'back' vector element - see the STL documentation
cout << "line read: " << lines_of_text.back() << "\n";
}
cout<<lines_of_text.size();
return 0;
}
答案 0 :(得分:2)
你拥有的代码是健全的。这是一个可能有用的小测试用例:
void read_lines(std::istream& input) {
using namespace std;
vector<string> lines;
for (string line; getline(input, line);) {
lines.push_back(line);
cout << "read: " << lines.back() << '\n';
}
cout << "size: " << lines.size() << '\n';
}
int main() {
{
std::istringstream ss ("abc\n\n");
read_lines(ss);
}
std::cout << "---\n";
{
std::istringstream ss ("abc\n123\n");
read_lines(ss);
}
std::cout << "---\n";
{
std::istringstream ss ("abc\n123"); // last line missing newline
read_lines(ss);
}
return 0;
}
输出:
read: abc
read:
size: 2
---
read: abc
read: 123
size: 2
---
read: abc
read: 123
size: 2
答案 1 :(得分:2)
我想我已经找到了问题的根源。在Code :: Blocks中,一个完全空的文件将报告IDE底部状态栏上的Gizmo中有一行(当前的一行)。这意味着如果您实际输入一行文本,它将是第1行。换句话说,Code :: Blocks通常会过度报告文件中的实际行数。你永远不应该依赖CB或任何其他IDE来查找文件信息 - 这不是它们的用途。
答案 2 :(得分:0)
好吧,如果文件的最后一行只是'\ n',则不要将其推入向量中。如果您希望它在那里,请将循环更改为:
while (getline( infile, textline, '\n' ).gcount() > 0)
{
if (infile.fail()) break; //An error occurred, break or do something else
// add the new element to the vector
lines_of_text.push_back( textline );
// print the 'back' vector element - see the STL documentation
cout << "line read: " << lines_of_text.back() << "\n";
}
使用gcount()
成员检查上次读取时读取的字符数 - 如果只读取分隔符,则返回1。
答案 3 :(得分:-1)
好的,这是一个你希望了解的解释。如果我们谈论的文件不以换行结束,那么您的代码应该可以正常工作。但如果它呢?让我们说它看起来像这样:
"line 1"
"line 2"
""
或者作为一系列字符:
line 1\nline 2\n
这个文件有三行 - 最后一行是空的,但它就在那里。在调用getline两次后,您已经读取了该文件中的所有字符。对getline的第三次调用会说oops,文件结束,不再抱歉,所以你只能看到两行文字。