c++忽略字符串参数

  • 本文关键字:参数 字符串 c++ c++
  • 更新时间 :
  • 英文 :


我已经构建了一个程序,用于创建,插入文本到。txt文件中,现在我需要添加一个小功能,我需要编写一个函数来显示目录中的文件列表。

int main() {
display_duration_start();
std::string VALID_COMMANDS[] = {"CREATE_FILE;","APPEND_TEXT;","DISPLAY_FILE;"};

int sum = 0;
int count = 1;
std::string insert;
while(std::getline(std::cin, insert) && insert != "quit") {
if (insert == "")
continue;

std::string command, item,item1,item2;
std::vector<std::string> items;
std::stringstream ss(insert);
if (!std::getline(ss, command, ' ')) {
std::cout << "Unknown command: `" + command + "`" << 'n';
break;
}

while (std::getline(ss, item, ' ')) {
items.push_back(item);
}
if (items.size() == 0) {
std::cout << "Usage: " + command + " <argument>" << 'n';
continue;
}

if(std::find(std::begin(VALID_COMMANDS), std::end(VALID_COMMANDS), command) == std::end(VALID_COMMANDS)) {
std::cout << "Command not found: " + command << 'n';
break;
}
for(auto& item: items) 
if(command == "CREATE_FILE;") {
create_file(item);
}
if(command == "APPEND_TEXT";) {
append_text(item);
}

if(command == "DISPLAY_FILE;") {
display_file();
}
}
}

现在,当我在控制台上输入DISPLAY_FILE;时,它会抛出这个:Usage: DISPLAY_FILE; <argument>。很明显,输出会产生,因为它需要一个参数,但是我如何使它不需要一个参数呢?

display_file()的功能(不确定是否有必要告知):

void display_file() {
struct dirent *d;
DIR *dr;
dr = opendir("C:\schema_location");
if(dr != NULL)
{
printf("");
for(d=readdir(dr); d!=NULL; d=readdir(dr))
{
printf("%sn", d->d_name);
}
closedir(dr);
}
else
printf("nERROR: Cannot retrieve file from the current directory");
}

您可以定义一个不需要参数的命令列表,并将其与是否存在参数一起检查。下面的代码应该使

工作
int main() {
//display_duration_start();
std::string VALID_COMMANDS[] = { "CREATE_FILE;","APPEND_TEXT;","DISPLAY_FILE;" };
std::string NO_ARGUMENT_COMMANDS[] = { "DISPLAY_FILE;" };
int sum = 0;
int count = 1;
std::string insert;
while (std::getline(std::cin, insert) && insert != "quit") {
if (insert == "")
continue;
std::string command, item, item1, item2;
std::vector<std::string> items;
std::stringstream ss(insert);
if (!std::getline(ss, command, ' ')) {
std::cout << "Unknown command: `" + command + "`" << 'n';
break;
}
while (std::getline(ss, item, ' ')) {
items.push_back(item);
}
if (items.size() == 0 &&
std::find(std::begin(NO_ARGUMENT_COMMANDS), std::end(NO_ARGUMENT_COMMANDS), command) == std::end(NO_ARGUMENT_COMMANDS)
) {
std::cout << "Usage: " + command + " <argument>" << 'n';
continue;
}
if (std::find(std::begin(VALID_COMMANDS), std::end(VALID_COMMANDS), command) == std::end(VALID_COMMANDS)) {
std::cout << "Command not found: " + command << 'n';
break;
}
for (auto& item : items)
{
if (command == "CREATE_FILE;") {
create_file(item);
}
if (command == "APPEND_TEXT";) {
append_text(item);
}
}
if (command == "DISPLAY_FILE;") {
display_file();
}
}
}
void display_file() {
struct dirent* d;
DIR* dr;
dr = opendir("C:\schema_location");
if (dr != NULL)
{
printf("");
for (d = readdir(dr); d != NULL; d = readdir(dr))
{
printf("%sn", d->d_name);
}
closedir(dr);
}
else
printf("nERROR: Cannot retrieve file from the current directory");
}

最新更新