如何检查字符变量是否为空?
我的意思是,如果字符串不包含任何字符empty()
则使用字符串stringVar.empty()
检查方法将导致 true。如何检查字符变量是否不包含字符?
例如,我有这样的代码:
// all libraries are included before and code is simplified because it is very long
std::fstream file;
char mychar;
file.open("userselectedfile.txt", std::fstream::in);
if (file.is_open() == true) {
while (file.eof() != true) {
// check if mychar is initialized yet (first time that the while execute)
if (mychar == '') {
mychar = file.get();
// do something special beacuse mychar wasn't initialized
// do something with other files
} else {
mychar = file.get();
// do something else with other files
}
}
}
file.close();
这段代码不正确,我不知道如何以一种好的方式解决,我发现了一件小事,让我绕过了这个问题,但它并不完美。目前我正在使用:
std::fstream file;
char mychar;
file.open("userselectedfile.txt", std::fstream::in);
if (file.is_open() == true) {
for (int i = 0; file.eof() != true; i++) {
if (i = 0) {
mychar = file.get();
} else {
mychar = file.get();
}
}
}
file.close();
这是检查 mychar 是否尚未初始化的唯一方法吗?如果有可能检查 mychar 是否尚未初始化与上述不同,我可以使用哪些函数?
更新 1
软件目标:在这种特定情况下,我正在构建的程序旨在删除用户(我的原因是为了我(提交的编码源代码文件中的每个评论,所以我不能使用特殊字符,因为它可以存在于文件中,使用