C++ 如何检查 char 变量是否未定义(未初始化)

C++ How to check if char variable is undefined (not initialized)

本文关键字:是否 未定义 初始化 变量 char 检查 C++ 何检查      更新时间:2023-10-16

如何检查字符变量是否为空?
我的意思是,如果字符串不包含任何字符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

软件目标:在这种特定情况下,我正在构建的程序旨在删除用户(我的原因是为了我(提交的编码源代码文件中的每个评论,所以我不能使用特殊字符,因为它可以存在于文件中,使用是个好主意,但我希望还有其他。当我在程序中编写//do something with ...时,我继续读取字符,直到找到注释,并且在创建没有注释的新文件时忽略它们。
物料清单:不,我测试的文件并没有让我忘记算法有点错误,但不是我问你的部分。

你不能。

在C++中,未初始化的变量具有未指定的值,以任何方式从中读取它们是未定义的行为,因此您甚至无法检查其中的内容。

您有三种选择:

  • 给它一个你知道不会在文件中遇到的值。可能适用于您的情况。
  • 使用单独的布尔变量来跟踪您是否读过一次。
  • 在 C++17 中,使用std::optional<char>

char 永远不会为空。 它始终包含一个值,即使您不初始化它。 如果不初始化它,就不能假设任何特定的值,所以你问的基本上是不可能的。

实际上,您可以将字符初始化为 NUL。 按照惯例,您可以假装表示"空"值。 完成此操作后,您可以测试以查看 char 是否包含空值,并将其用作测试的基础。

char x = '';   // Initialize to 'empty' value
char y = 'a';    // Initialize to a 'non-empty' value
if (x == '') std::cout << "x is empty" << std::endl;
if (y == '') std::cout << "y is empty" << std::endl;
std::fstream::get()

不返回字符,而是返回整数。 除了其他人提供的解决方案外,您还可以使用int mychar = -1并检查一下。

但是,我强烈建议您重新组织代码,这样您就不必这样做了。事实上,如果您正确解析get()的结果,则在使用get()之前不需要(也不应该使用(对 EOF 进行额外的检查。它精确地返回一个 int,以便在无法读取字符时可以返回EOF而不是 char 值。

最后,您还可以使用另一个重载的get(),它将目标字符作为参数,如下所示:

while (file.get(mychar)) { … }

如果你有一个变量可以保存一个值,也可以不保存一个值,那么std::optional就是为此目的而制作的。举个例子:

#include <iostream>
#include <optional>
void print_or_not(std::optional<char> x){
if (x) std::cout << x.value();
}
int main() {
std::cout << "uninitialized n";
std::optional<char> x;
print_or_not(x);
std::cout << "initialized n";
x = 'x';
print_or_not(x);
}

指纹

uninitialized 
initialized 
x

与其尝试确定mychar是否尚未使用,不如让我们重构循环以为您处理它。 如果你使用 for 循环,你可以声明一个变量,我们可以用它来跟踪它是否是循环的第一次迭代。 这将使代码看起来像

for(bool first_iteration = true; file.eof() != true;) {
mychar = file.get();
// check if this is the first iteration
if (first_iteration) {
first_iteration = false;
// do something special beacuse this is the first iteration
}
// do something with other files
}

你不能。如果要在第一次执行循环时执行不同的操作,请使用布尔标志:

bool first = true;
while (file.eof() != true) {
if (first) {
// do something
first = false;
} else {
// do something else
}
}

如果您确实想要一个可能包含也可能不包含 char 值的变量,请使用std::optional<char>

您可以使用以下代码检查 char 变量是否未定义(未初始化(:

char c;
if(c == NULL)
{
cout<<"Your character doesn't have any value";
}
else
{
cout<<"the character you entered is:" << c;
}