简体   繁体   中英

Reading words from a text file: weird behaviour

I'm running the following program

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

using namespace std;

int main(int argc, char *argv[])
{
    ifstream input_file(argv[1]);
    vector<string> words;
    string line;

    while(getline(input_file, line))
    {
        cout << line << endl;
        words.push_back(line);
    }
    input_file.close();

    cout << "First and last word: " << words[0] << " " << words.back() << endl;

    return 0;
}

using the following text file as an input

permission
copper
operation
cop
rationale
rest

and I get the following output in terminal

permission
copper
operation
cop
rationale
rest

 rest and last word: permission

Why is the last word words.back() printed at the beginning of the line while erasing some of the text?

Because your file has Windows line-endings ( "\r\n" ) and you're on Linux or Mac (which doesn't translate these into "\n" ).

std::getline is only trimming the '\n' s for you. So, \r is left at the end of each of your strings; in many consoles, '\r' moves the write cursor to the start of the line. Then, the " " << words.back() part overwrites the already-written "First and last word: " << words[0] part.


Example:

  • First word is permission␍
  • Last word is rest␍

(Note the control character at the end of each word!)

┌───────────────────┬──────────────────────────────────────┐
│                   │  ⭭⭭⭭⭭⭭                               │
│ Write "First"     │  First                               │
│                   │       ꕯ                              │
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┼┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┤
│                   │       ⭭⭭⭭⭭                           │
│ Write " and"      │  First·and                           │
│                   │           ꕯ                          │
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┼┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┤
│                   │           ⭭⭭⭭⭭⭭                      │
│ Write " last"     │  First·and·last                      │
│                   │                ꕯ                     │
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┼┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┤
│                   │                ⭭⭭⭭⭭⭭⭭                │
│ Write " word:"    │  First·and·last·word:                │
│                   │                      ꕯ               │
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┼┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┤
│                   │                      ⭭⭭⭭⭭⭭⭭⭭⭭⭭⭭⭭␍    │
│ Write first word  │  First·and·last·word:·permission     │
│                   │  ꕯ                                   │
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┼┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┤
│                   │  ⭭                                   │
│ Write " "         │  ·irst·and·last·word:·permission     │
│                   │   ꕯ                                  │
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┼┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┤
│                   │   ⭭⭭⭭⭭␍                              │
│ Write last word   │  ·rest·and·last·word:·permission     │
│                   │  ꕯ                                   │
└───────────────────┴──────────────────────────────────────┘

Solution

You can strip this from the end of each line yourself, or preprocess the file externally .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM