getline help

Hello all,

My issue is when I run this code in VS code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream fin;

    fin.open("text.txt");

    string line = "";
  
    while(getline(fin,line))
    {
        cout << line;
    }
}


my terminal shows the following:
Whos fleece was white as now

However when I run it in Visual Studio, the window that pops up shows:
Mary has a little lambWhos fleece was white as snow

I know the second line should be two separate sentences, but why am I losing "Mary had a little lamb" in the first instance?

This is the format of the file i'm reading from
1
2
Mary had a little lamb
Whos fleece was white as snow


Thanks in advance.
Last edited on
cout << line << endl;
Hello noblemin,

First you should provide the input file. That will let others know what it looks like and there could be something in the file that you may not see. It helps when everyone can use the same information.

You are using "std::string" and "std::getline" in the program, but you did not include the "string" header file to cover these. The compiler should have stopped with errors about these. If not you may need to turn up the warning level.

It also helps to mention which version of VS you are using, 2015,2017 or 2019.

I am not familiar with VS Code and how it works, but I am thinking it should work much the same as the VS IDEs.

On line 11 you open a file stream "fin", but how do you know it is open?

When you open a file for input it is a MUST that you check it.

The following code will demonstrate that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    const std::string inFileName{ "text.txt" };

    ifstream fin(inFileName);

    //fin.open("text.txt");

    if (!fin)
    {
	std::cerr << "\n     File \"" << inFileName << "\" did not open!\n";

        return 1;
    }

    string line;

    while (getline(fin, line))
    {
        cout << line << '\n';
    }

    return 0;  // <--- Not required, but makes a good break point.
}

Line 11 shows you how you can define the variable name and open the file at the same time. This is considered the preferred method, but if you want or need touse the "open" line 13 that is OK.

Lines 15 - 20 are what you need to check if the file stream is open and ready for use.

You do not need to initialize a "std::string".
1
2
string line = "";
string line;

Both of these are the same. They both produce a string with a zero size. Adding ( = "") has no advantage.

Andy

Edit: in regards to the last line this is based on what I see in VS2017.
Last edited on
I think it's likely that your input file was created on an Operating System that uses different characters to delimit a newline. For example, if the file was created on Windows and you the program on Linux, then I'd expect the exact behavior you're seeing.

In Windows, a newline is two characters (\r\n). In Linux, it's just \n. So if you read the first line on a Linux computer, you'll actually get "Mary had a little lamb\r". And when you print it to the terminal, it writes "Mary had a little lamb" and then \r positions the cursor back at the beginning of the line.

Now you read and print "Whos fleece was white as snow" and that overwrites the first line.

To test this theory, temporarily reverse the two lines. You'll probably see
Mary had a little lambas snow

Topic archived. No new replies allowed.