遇到空格时如何停止阅读一行?
How do I stop reading a line when I encounter a whitespace?
我目前正在打开一个文件并使用 getline(x,y) 读取它,但我想在遇到空格时停止读取该行,然后从那里继续读取。
谢谢
格式化输入将停在白色 space:
std::string str;
while( file >> str)
std::cout << str << std::endl;
也许您正在寻找 isspace( int ch)
方法:
#include <cctype>
if ( isspace( string.at(i)))
// ... stop
所以你可能想从文件中读取每个字符并用 if
语句测试它是否等于 whitespace
,所以看看函数 getc
.
我目前正在打开一个文件并使用 getline(x,y) 读取它,但我想在遇到空格时停止读取该行,然后从那里继续读取。 谢谢
格式化输入将停在白色 space:
std::string str;
while( file >> str)
std::cout << str << std::endl;
也许您正在寻找 isspace( int ch)
方法:
#include <cctype>
if ( isspace( string.at(i)))
// ... stop
所以你可能想从文件中读取每个字符并用 if
语句测试它是否等于 whitespace
,所以看看函数 getc
.