简体   繁体   English

C ++ cin没有得到线

[英]C++ cin didn't get line

I have a program where I'm specifying the file to be read using cin, so I run the program ./prog < file.txt, but in the following code, cin doesn't grab anything. 我有一个程序,我指定要使用cin读取的文件,所以我运行程序./prog <file.txt,但在下面的代码中,cin不会抓取任何东西。 Could someone explain why line is empty after the code executes? 有人可以解释为什么代码执行后行是空的吗?

 void Building::build(){
    char mode;
    cin >> mode >> sizeFloors >> numFloors;

    if(mode == 'M')
        readMap(sizeFloors, numFloors);

}

^^ this executes fine ^^这个执行得很好

void Building::readMap(int floorSize, int numFloors){

    string line;
    int curFloor(numFloors - 1);

    while( curFloor >= 0 ){
        cin >> line;

        if(line.empty()){
            cout << "Error: input file too short" << endl;
            exit(1);
        }
    }   

^^ here line.empty() returns true ^^这里line.empty()返回true

this is the input file 这是输入文件

M
4
1
WWWW
WWWW
WWWW
WWWW

so clearly line shouldnt return empty 所以显然行不应该返回空

When mixing formatted input (ie, using operator>>() ) and unformatted input (eg, std::getline() ) you need to make sure that you are at a location you are interested in. The formatted input operators stop reading the moment their format is satisfied. 当混合格式化输入(即使用operator>>() )和无格式输入(例如, std::getline() )时,您需要确保您处于您感兴趣的位置。格式化的输入运算符停止读取他们的格式满足的那一刻。 For example, reading a character just reads that one character. 例如,读取一个字符只读取一个字符。 Any following charactters, eg, a newline, is left in the input. 在输入中留下任何以下字符,例如换行符。 std::getline() stops reading at the first newline received. std::getline()在收到的第一个换行符处停止读取。 I guess, after you entered your menu selection you hit newline and this is where std::getline() stops (same if the menu selection is in a file on its own). 我想,在你输入菜单选项后,你会看到换行符,这就是std::getline()停止的地方(如果菜单选择std::getline()在一个文件中,则相同)。

A typical approach when switching between formatted and unformatted I/O is to skip all leading spaces: 在格式化和未格式化I / O之间切换时的典型方法是跳过所有前导空格:

std::getline(std::cin >> std::ws, line);

Alternatively, you can ignore everything up to and including the first newline: 或者,您可以忽略所有内容,包括第一个换行符:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Which approach is used clearly depends on the content of your data. 使用哪种方法显然取决于数据的内容。 For example, if you want to read code where leading namespace matters skipping up to the first non-whitespace is not an award winning idea. 例如,如果您想要读取前导命名空间很重要的代码,那么跳过第一个非空白区域并不是一个获奖的想法。 However, in many situations using std::ws works just fine. 但是,在很多情况下使用std::ws工作得很好。

This might help you ... 这可能对你有所帮助......

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

The >> operator, when reading a std::string is taking spaces as string separator, see here . >>运算符,当读取std::string将空格作为字符串分隔符,请参见此处

You might want to use getline instead. 您可能希望使用getline

And you might (eg on Linux) compile your code with all warnings and debugging information (eg with g++ -Wall -g ) then use a debugger (eg gdb ) to find out what is happening. 您可能(例如在Linux上)使用所有警告和调试信息(例如使用g++ -Wall -g )编译代码,然后使用调试器(例如gdb )来查找正在发生的事情。

I would suggest to line.clear() inside the loop. 我建议在循环内部使用line.clear()

I also find writing int curFloor(numFloors - 1); 我也发现写int curFloor(numFloors - 1); less readable than int curFloor = numFloors - 1; int curFloor = numFloors - 1;更不易读int curFloor = numFloors - 1;

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

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