<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/57946>57946</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`<fstream>`: Potential glitch with `ifstream::peek` in text mode when the file contains LF line endings on Windows
</td>
</tr>
<tr>
<th>Labels</th>
<td>
libc++,
platform:windows
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
fsb4000
</td>
</tr>
</table>
<pre>
```c++
#include <cassert>
#include <fstream>
#include <iostream>
using namespace std;
int main() {
const char* const filename = "example_file.txt";
{
ofstream o(filename, ios_base::binary);
assert(o.good());
o << "cute fluffy kittens\n";
}
ifstream fin(filename);
fin.rdbuf()->pubsetbuf(nullptr, 0);
assert(fin.good());
cout << "start pos: " << fin.tellg() << '\n';
assert(fin.good());
cout << "fin.peek(): " << static_cast<char>(fin.peek()) << '\n';
assert(fin.good());
cout << "end pos (expected 0): " << fin.tellg() << '\n';
}
```
```
$ clang-cl.exe --driver-mode=g++ --target=x86_64-pc-windows-msvc -nostdinc++ -I C:/Dev/STL/llvm-project/build/include/c++/v1 -std=c++2b -llibc++experimental -nostdlib -L C:/Dev/STL/llvm-project/build/lib -lc++ -lmsvcrt -lmsvcprt -loldnames -g main.cpp
$ a.exe
start pos: 0
fin.peek(): c
end pos (expected 0): -1
```
Same result with mingw-w64:
```
$ clang++ -stdlib=libc++ main.cpp
$ ./a
start pos: 0
fin.peek(): c
end pos (expected 0): -1
```
clang + libstdc++ gives the zero:
```
$ clang++ main.cpp
$ ./a
start pos: 0
fin.peek(): c
end pos (expected 0): 0
```
without the `fin.rdbuf()->pubsetbuf(nullptr, 0);` the result is also zero.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9VV2PmzgU_TXwYoHABBIeeJhOdqSV5mGlVurjyJgL8dbYCJsks79-rw1MJmln1FZtEUK2L_Y959wP17p5roIimV8e0A_uTfZBchfQTCgupwZ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