简体   繁体   English

为什么 cin.getline() 跳过每一行中的第一个单词?

[英]Why is cin.getline() skipping the first word in each line?

I am trying to display the text of a command line inputted text file line by line.我正在尝试逐行显示命令行输入的文本文件的文本。 But for some reason, it skips the first word in each line after the first line.但出于某种原因,它会跳过第一行之后每行的第一个单词。

code:代码:

using std::cout;  
using std::cin;  
using std::endl;  

int main (int args, char* argv[])  
{   
 char x[100];  
 char y[100];  
 char z[100];  
 cin.getline(x,100) >> argv[2];  
 cin.getline(y,100) >> argv[2];  
 cin.getline(z,100) >> argv[2];  
 cout << x <<endl;  
 cout << y <<endl;  
 cout << z <<endl;  
 return 1;  
}  

running ./a.out < moby.txt displays this:运行 ./a.out < moby.txt 显示:

CHAPTER 1. Loomings. 

me Ishmael. Some years ago--never mind how long precisely--having  
or no money in my purse, and nothing particular to interest me on

but the first three lines in moby.txt is this:但是 moby.txt 中的前三行是这样的:

CHAPTER 1. Loomings.

Call me Ishmael. Some years ago--never mind how long precisely--having  
little or no money in my purse, and nothing particular to interest me on

The code is omitting "Call" and "little".代码省略了“Call”和“little”。
I feel like this is an \\n error but i have no idea how to fix it.我觉得这是一个 \\n 错误,但我不知道如何修复它。 Thanks in advance for any help.在此先感谢您的帮助。

cin.getline(x,100) >> argv[2];

You read a line (or the first 99 characters of the line) into x .您将一行(或该行的前 99 个字符)读入x Then you skip any whitespace and read the next word into argv[2] .然后跳过任何空格并将下一个单词读入argv[2] The first words are ending up there.第一个词在那里结束。

Why are you using >> argv[2] ?你为什么使用>> argv[2] What are you possibly trying to do with this?你可能想用这个做什么? argv[2] may not exist and even if it does, you don't have any control over the size of the character array pointed to by argv[2] , so your chances of overrunning that array are quite high. argv[2]可能不存在,即使存在,您也无法控制argv[2]指向的字符数组的大小,因此超出该数组的可能性非常高。

Rather than using char arrays directly for this, use std::getline with std::string to read lines into std::string objects: it is much easier to write correct code this way.与其直接为此使用char数组,不如使用std::getlinestd::string将行读入std::string对象:以这种方式编写正确的代码要容易得多。 For example,例如,

std::string x;
if (!std::getline(std::cin, x)) {
    // handle input error
}

@James McNellis has already pointed to the basic problem. @James McNellis 已经指出了基本问题。 My advice would be:我的建议是:

  1. Don't use the member-function form of getline .不要使用getline的成员函数形式。
  2. Don't mix getline and >> in the same statement.不要在同一个语句中混用getline>>
  3. Use a loop.使用循环。

I find the other C++ getline to be easier and safer to use;我发现另一个 C++ getline 使用起来更容易、更安全;

string str;
getline (cin,str);

will slurp the entire line and put it into a string, which you can then play with via the many fine string methods, or stringstream if you want to do I/O on parts of the string.将整行并将其放入一个字符串中,然后您可以通过许多精细的字符串方法进行播放,或者如果您想对字符串的某些部分进行 I/O,则可以使用 stringstream。

This is what I chose to do to make sure I am not missing any words or letters when I Use getline :这是我选择做的,以确保我在使用getline时不会遗漏任何单词或字母:

cout << "\nEnter some words: ";

while (getline(cin,myString)){
       getline(cin,myString);
       break;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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