首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从文本文件中读取单词:奇怪的行为

从文本文件中读取单词:奇怪的行为
EN

Stack Overflow用户
提问于 2020-08-04 13:57:01
回答 1查看 71关注 0票数 3

我正在运行以下程序

代码语言:javascript
复制
#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;
}

使用以下文本文件作为输入

代码语言:javascript
复制
permission
copper
operation
cop
rationale
rest

我在终端中得到以下输出

代码语言:javascript
复制
permission
copper
operation
cop
rationale
rest

 rest and last word: permission

为什么在删除部分文本时,最后一个单词words.back()会打印在行的开头?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-04 14:04:02

因为您的文件有Windows ("\r\n"),并且您在Linux或Mac上(这不会将它们转换为"\n")。

std::getline只为您修剪'\n's。因此,\r留在每个字符串的末尾;在许多控制台中,'\r'将写光标移动到行的开头。然后," " << words.back()部件覆盖已经写入的"First and last word: " << words[0]部件。

示例:

permission␍

  • Last是
  • 第一个单词是rest␍

(请注意每个单词末尾的控制字符!)

代码语言:javascript
复制
┌───────────────────┬──────────────────────────────────────┐
│                   │  ⭭⭭⭭⭭⭭                               │
│ 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     │
│                   │  ꕯ                                   │
└───────────────────┴──────────────────────────────────────┘

解决方案

or preprocess the file externally,您可以自己从每一行的末尾删除它。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63248485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档