linux 交互简单实例

#include <stdio.h>
#include <string.h>
#include <sys/types.h>    
#include <sys/stat.h>    
#include <fcntl.h>

typedef struct {
    int  cmdIdx;
    char *funcName;
} test_case_info;

test_case_info test_case[] = 
{
    {0,    "test_case1"},
    {1,    "test_case2"},
    {2,    "test_case3"},
    {3,    "test_case4"},
    {4,    "test_case5"},
    {-1,    NULL}
};

void print_help(void)
{
    int i = 0;
    printf("Supported test cases:\n");
    while (test_case[i].cmdIdx != -1) {
        printf("%d:\t%s\n", test_case[i].cmdIdx, test_case[i].funcName);
        i++;
    }
}


int main(int argc, char *argv[])
{
    int cmd_index  = 0;
    int ret = 0;

    print_help();
    while (1) {
        printf("please input cmd index(exit_index:-1): ");
        fflush(stdin);
        scanf("%d", &cmd_index);
        printf("cmd_index:%d\n", cmd_index);

        if (cmd_index == -1)
            break;

        switch (cmd_index) {
            case 0: {
                char temp[32] = {0};
                int flag = 0;
                int string_length = sizeof(temp);

                printf("please input file name[%d]: \n", string_length);
                fflush(stdin);
                scanf("%s", temp);

                printf("please input file flag(1:O_RDWR, 2:O_RDONLY ): \n");
                fflush(stdin);
                scanf("%d", &flag);
                switch (flag) {
                    case 1:
                        flag = O_RDWR;
                        break;

                    case 2:
                        flag = O_RDONLY;
                        break;

                    default:
                        break;
                }

                string_length = strlen(temp);
                printf("content:%s, length:%d\n", temp, string_length);
                break;
            }

            default: {
                print_help();
                break;   
            }       
        }
    }


    return 0;
}

























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值