What is the C++ equivalent to the C function fgets?
I have looked at getline from ifstream, but when it comes to an end of line character, '\n', it terminates at and discards it. I am looking for a function that just terminates at the end line character but adds the end of line character to the char array.
You can still use std::getline(); just append the newline character yourself. For example,
std::ifstream fs("filename.txt");
std::string s;
std::getline(fs, s);
// Make sure we didn't reach the end or fail before reading the delimiter:
if (fs)
s.push_back('\n');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With