[英]reading char by char
我的问题有点奇怪,但是如何仅通过使用operator<<
就可以不使用scanf()
和getchar()
从键盘char中读取一些字符串,例如,我想更改读取的每个字母a
通过*
预先感谢
char c;
std::string output;
while(std::cin >> c)
{
if(c == 'a')
c = '*';
output += c;
}
Depending on your purposes, you may find it adequate to do this: 根据您的目的,您可能会发现这样做足够:
#include <iostream>
#include <iomanip>
char c;
std::cin >> std::noskipws;
while (std::cin >> c)
std::cout << c == 'a' ? '*' : c;
See http://www.cplusplus.com/reference/iostream/manipulators/noskipws/ for further details of noskipws, which is crucial as otherwise operator>> will skip over spaces, tabs and newlines until it finds other characters to put in 'c', resulting in all the aforementioned whitespace characters being removed from your output. 有关noskipws的更多详细信息,请参见http://www.cplusplus.com/reference/iostream/manipulators/noskipws/ ,这是至关重要的,否则操作符>>会跳过空格,制表符和换行符,直到找到要放入' c',导致所有上述空白字符从您的输出中删除。
Try writing out to a type of char. 尝试写出一种char类型。
char cReadFromStream;
stream >> cReadFromStream;
You cannot. 你不能。
operator <<
uses cooked inputs. operator <<
使用煮熟的输入。
However, if you really meant you are looking for a solution using C++ iostreams and not C stdio functions, then use cin.get()
which is the C++ iostreams equivalent to getchar
. 但是,如果您确实要寻找使用C ++ iostreams而不是C stdio函数的解决方案,请使用
cin.get()
,它是等效于getchar
的C ++ iostreams。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.