在我的程序中,我必须解析用户的命令,然后执行适当的功能。但是,我想知道如何检查用户输入是否为空。如果为空,我会给他一条消息以正确输入命令。以下部分是我的命令解析器,它与scanf一起使用strcmp。
void CommandParser(const char* arg_computer_name)
{
PrintColorful(2, "%s%s\n\n", "You are currently at c:\\", g_c_SystemModifiablePath);
PrintColorful(0, "%s [%s%s] %s ", arg_computer_name, g_c_RootDrive, g_c_SystemModifiablePath, FM_CMD);
while (scanf_s("%s", g_c_Commands, MAX_PATH - 1))
{
if (!strcmp(g_c_Commands, "help") || !strcmp(g_c_Commands, "?"))
{
FmShowHelpMessage();
}
else if (!strcmp(g_c_Commands, "version"))
{
FmShowProgramVersion();
}
else if (!strcmp(g_c_Commands, "cd"))
{
FmCommandChangeDirectory(arg_computer_name);
}
...
else
{
ErrorMessage("%s\n", "The command isn't supported.");
}
PrintColorful(0, "%s [%s%s] %s ", arg_computer_name, g_c_RootDrive, g_c_SystemModifiablePath, FM_CMD);
}
}