检查行是否为空或包含不可打印的字符
Check if line is empty or contains non pritable characters
如何检查从文件中扫描的行是否为空或包含不可打印的字符?我尝试在 getline 的结果上使用 strlen() ,当有空行但不可打印的字符破坏此代码时,它等于 1。我怎样才能做得更好?
如果if是C代码那么可以自己写对应的函数
int isValid( const char *s )
{
while ( *s && !isgraph( ( unsigned char )*s ) ) ++s;
return *s != '[=10=]';
}
如果它是 C++ 代码并且您使用的是字符数组,那么您可以使用以下方法
#include <algorithm>
#include <iterator>
#include <cctype>
#include <cstring>
//...
if ( std::all_of( s, s + std::strlen( s ), []( char c ) { return !std::isgraph( c ); } ) )
{
std::cout << "Invalid string" << std::endl;
}
对于 std::string
类型的对象,检查看起来类似
if ( std::all_of( s.begin(), s.end(), []( char c ) { return !std::isgraph( c ); } ) )
{
std::cout << "Invalid string" << std::endl;
}
如何检查从文件中扫描的行是否为空或包含不可打印的字符?我尝试在 getline 的结果上使用 strlen() ,当有空行但不可打印的字符破坏此代码时,它等于 1。我怎样才能做得更好?
如果if是C代码那么可以自己写对应的函数
int isValid( const char *s )
{
while ( *s && !isgraph( ( unsigned char )*s ) ) ++s;
return *s != '[=10=]';
}
如果它是 C++ 代码并且您使用的是字符数组,那么您可以使用以下方法
#include <algorithm>
#include <iterator>
#include <cctype>
#include <cstring>
//...
if ( std::all_of( s, s + std::strlen( s ), []( char c ) { return !std::isgraph( c ); } ) )
{
std::cout << "Invalid string" << std::endl;
}
对于 std::string
类型的对象,检查看起来类似
if ( std::all_of( s.begin(), s.end(), []( char c ) { return !std::isgraph( c ); } ) )
{
std::cout << "Invalid string" << std::endl;
}