microsoft / microsoft/STL

`<fstream>`: Potential glitch with `ifstream::peek` in text mode when the file contains LF line endings

Open
#2,477 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Originally reported as DevCom-1628547 and internal VSO-1458783 / AB#1458783 .

C:\Temp>type meow.cpp
#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);
    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';
}
C:\Temp>cl /EHsc /nologo /W4 meow.cpp
meow.cpp

C:\Temp>meow
start pos: 0
fin.peek(): c
end pos (expected 0): -1

I don't know whether this is by design.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling and running the provided C++ reproduction on Windows, focusing on ifstream::peek() and the subsequent tellg() result with LF line endings. Compare the observed position with the expected behavior and determine whether the implementation or a regression test needs updating.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
operating-systems, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.