You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让C++程序中DISPLAY_FILE;命令无需参数即可执行?

解决C++程序中DISPLAY_FILE;命令无需参数即可执行的问题

我开发了一款支持创建文件、向TXT文件追加文本的C++程序,新增了display_file()函数用于显示指定目录的文件列表。但在控制台输入DISPLAY_FILE;命令时,程序会提示Usage: DISPLAY_FILE; <argument>——当前逻辑强制所有命令必须携带参数,需要修改代码让DISPLAY_FILE;无需参数就能正常执行。

原代码

主函数代码

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()函数代码

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("%s\n", d->d_name);
        }
        closedir(dr);
    }
    else
        printf("\nERROR: Cannot retrieve file from the current directory");
}

修改方案

1. 调整参数检查逻辑

原代码中只要没有参数就提示用法错误,我们需要仅对确实需要参数的命令(CREATE_FILE;、APPEND_TEXT;)做参数检查,让DISPLAY_FILE;跳过这一步:

替换原参数检查代码:

// 原代码
if (items.size() == 0) {
    std::cout << "Usage: " + command + " <argument>" << '\n';
    continue;
}

改为:

// 仅对需要参数的命令验证参数是否存在
if ((command == "CREATE_FILE;" || command == "APPEND_TEXT;") && items.empty()) {
    std::cout << "Usage: " << command << " <argument>" << '\n';
    continue;
}

2. 修正命令执行的逻辑结构

原代码中for循环的作用域存在问题,APPEND_TEXT;和DISPLAY_FILE;的执行逻辑不在循环内,且DISPLAY_FILE;不需要遍历参数。调整后让每个命令的逻辑更清晰:

替换原命令执行代码:

// 原代码
for(auto& item: items) 
    if(command == "CREATE_FILE;") {
        create_file(item);
    }

    if(command == "APPEND_TEXT;") {
        append_text(item);
    }
    
    if(command == "DISPLAY_FILE;") {
        display_file();
    }

改为:

if(command == "CREATE_FILE;") {
    // 遍历参数创建多个文件
    for(auto& item : items) {
        create_file(item);
    }
} else if(command == "APPEND_TEXT;") {
    // 遍历参数向多个文件追加文本
    for(auto& item : items) {
        append_text(item);
    }
} else if(command == "DISPLAY_FILE;") {
    // 无需参数,直接执行目录展示
    display_file();
}

修改后的完整主函数代码

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;
        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 ((command == "CREATE_FILE;" || command == "APPEND_TEXT;") && items.empty()) {
            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;
        }

        if(command == "CREATE_FILE;") {
            for(auto& item : items) {
                create_file(item);
            }
        } else if(command == "APPEND_TEXT;") {
            for(auto& item : items) {
                append_text(item);
            }
        } else if(command == "DISPLAY_FILE;") {
            display_file();
        }
    }
}

内容的提问来源于stack exchange,提问作者Pythonic User

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 05:24:27