I use std::fstream to read text files line by line with the member function getline(). It uses buffer of limited size, and sometimes lines on input are longer than the buffer. After the first reading of the long line getline() stopped read data, what was surprise for my after the debug of the code. The documentation states, that reading of the long line set the error flag **failbit , **it is described as usual situation, not runtime error or fatal failure (https://cplusplus.com/reference/istream/istream/getline/). Documentation does not point any attention to this flag and does not describe, how to deal with long lines.
Do you know any documented way, how to precess long lines ? It should be, as this behaviour affect all class std::fstream, but I never see any descriptions or discussions.
The example of the program:
#include <fstream>
#include <iostream>
#include <string>
int main(){
char buffer[10];
std::string filename{"test.txt"};
auto file=std::fstream(filename, std::ios_base::in | std::ios_base::binary );
if(file.is_open()){
size_t readBites{4};
file.getline(buffer,readBites);
auto length=std::strlen(buffer);
auto lengthStream=file.gcount();
std::cout << "Read "<<length<<"/"<<lengthStream<<":"<<buffer<<std::endl;
file.getline(buffer,readBites);
length=std::strlen(buffer);
lengthStream=file.gcount();
std::cout << "Read "<<length<<"/"<<lengthStream<<":"<<buffer<<std::endl;
}
return 0;
}
If test.txt file contains, for example:
1234567890
ABCDEFGHIJK
I expected output (like fgets(...) in C):
Read 3/3:123
Read 3/3:456
but it really is :
Read 3/3:123
Read 0/0:
I use limited size of buffer, as it is requirement of the client software, calling this code, it can be any size and processes long lines in short buffer.
As the documentation you linked to says:
So, if you try to read more data than your buffer can hold, the stream's
failbitstate is set:At that point, the stream won't read any more data:
Since the stream is no longer in a
goodstate, it stops extracting characters. You have toclear()the stream to return it to agoodstate.