How to handle carriage or newline character

When I try to read from a file, the last value is read twice if'Enter' is used at the end of file to create a new line.

The code is as follows:

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
#include <iostream>
#include <vector>
#include <string>

using namespace std;



int main()
{
        ifstream imap("imap");
        vector<string> vmap;
        string key;

        while (!imap.eof())
       {
               imap >> key;
               if (key != "\r")
	         vmap.push_back(key);
       }
       imap.close();
       
       return 0;
}



If I have file in such format:
red
red [ENTER to create a new line in file]

Vector will be filled with three red.Seems it needs some special handling when read using ifstream when there is a carriage or newline.

Thanks for any suggestion or solution,

Last edited on
It works, great. Thanks
Topic archived. No new replies allowed.