0

I used this code to read lines from file, but I noticed, that it didn't read line breaks:

ifstream fs8(sourceFile);
string line;

while (getline(fs8, line))
{
   //here I am doing convertation from utf8 to utf16, but I need also to convert symbol "\n"
}

How to read line with line breaks ?

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
VFbvb
  • 11
  • 5
  • Possible duplicate of [reading a line from ifstream into a string variable](https://stackoverflow.com/questions/6663131/reading-a-line-from-ifstream-into-a-string-variable) – Sudheesh Singanamalla Jan 27 '18 at 16:19

1 Answers1

0

std::getline() reads data up to a delimiter, which is not stored. By default, that delimiter is '\n'. So you would have to either:

a) Pick a different delimiter -- but then you would no longer read "lines".

b) Add the newline to the data read (line += '\n').

I'd go for b), if you really need that newline converted. (I don't quite see why that would be necessary, but who am I to judge. ;-) )

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • but maybe there is some other function, which automatically read line with delimeter ? – VFbvb Jan 27 '18 at 14:41
  • @VFbvb: Not to my knowledge, not for C++ style iostreams. As adding the newline / delimiter manually is generally easier (and less often desired) than removing it, I don't see why there *should* be, anyway. C style `fgets()` stores the newline, but I don't think it's worth the hassle of handling `char` arrays and checking for incomplete reads. Add the newline yourself and be done with it. – DevSolar Jan 27 '18 at 15:37