-2

So I've been learning about steams and have been experimenting on my own. I was attempting to write a simple program to read the first line of a file only. But I've noticed an issue in Visual Studio 2019. Below is the code snippet.

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream in("Test.txt", std::ios::binary);
    if (in)
    {
        std::string store;
        std::getline(in, store, '\n');
        std::cout << store;
    }
    return 0;
}

The test.txt file reads:

This
Is
A
Test

In VS19 the output to the console is just " his". In VSCode the output is "This" as it should be. Am I doing anything wrong in VS19 as far as possible configuration? Is it a bug? It was driving me crazy until I tried a different IDE, can't figure out why it would be doing that. Note: It isn't ignoring the first character of the string, it is replacing it with whitespace.

Hex of Test.txt for VS2019

00000000    54 68 69 73 0D 0A 49 73 0D 0A 41 0D 0A 54 65 73 
00000010    74

And VSCode

00000000: 54 68 69 73 0A 49 73 0A 41 0A 54 65 73 74
34
  • 1
    Questions seeking debugging help should generally include a minimal reproducible example of the problem, which includes a function main and all #include directives. Commented Jan 8, 2022 at 7:43
  • 2
    Is string a custom class? Or are you doing something very bad as including <bits/stdc++.h> and using namespace std;. If so: there's your problem. Commented Jan 8, 2022 at 8:48
  • 3
    Out of curiosity, why you're reading a text file in as binary? On some platforms, specifically Windows, line termination translation will be affected by that. It may also be worth your time to actually check that file for a possible BOM (stranger things). Commented Jan 8, 2022 at 8:52
  • I'm guessing you're using a different compiler in vscode? It's basically just a text editor so isn't relevant to your question. Please show a minimal reproducible example. My guess would be some sort of encoding issue, especially as you are opening in binary mode Commented Jan 8, 2022 at 9:35
  • 1
    @JHBonarius if the question was answerable I would write an answer. Why is auto [const] name{type{}}; better than type [const] name{}; Commented Jan 8, 2022 at 10:34

1 Answer 1

0

You are opening your input file in binary mode. This means that getline will read the following bytes (it will discard the newline character):

54 68 69 73 0D

This corresponds to the following string:

"This\r"

The last character is the carriage-return character.

In the comments section of the question, you stated that you experienced the same problem when you simply wrote

std::cout << "This\r";

to the console. In that case, the character T was also being overwritten. This probably means that this is simply the way that your console handles the carriage-return character.

The best way to fix this problem is to open the text file in text mode, instead of binary mode. That way, the "\r\n" line endings will automatically be converted to "\n" line endings, and you won't have to deal with any carriage-return characters.

Therefore, I suggest that you change the line

std::ifstream in("Test.txt", std::ios::binary);

to:

std::ifstream in("Test.txt");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.