How to read from std::cin until the end of the stream?

亡梦爱人 提交于 2019-11-29 15:47:48

You could require the input to always end EOF (meaning from commmand line requiring ^D to be pressed) and then use the process for b as always. This would then enable multiline input from cmdline as well

You could use the (old) getline function, which takes a pointer to a char array and can use a delimiter. But you wont be able to ensure that in every case it will read to the eof (as the char buffer might be too small), but using a char-array and not paying attention to the size of it is a very dangerous (and from the point of security catastrophic) thing anyways as it can lead to buffer-overflows which can be easily exploited.

This code should enable you to extract line by line from the input:

char buffer[256];
while(getline(buffer,256,'\n')) { //get a line
   /* do something with the line */
}

This to read a maximum amount of chars:

char buffer[256];
while(getline(buffer,256)) {//get a maximum amount out of the input
   /* do something with the line */
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!