鱼C论坛

 找回密码
 立即注册
查看: 1020|回复: 2

如何分离出cat<1.txt>2.txt>3.txt的各部分

[复制链接]
发表于 2023-9-26 19:50:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
需要将char command[1024]="cat<1.txt>2.txt>3.txt" 分离成 cat < 1.txt > 2.txt > 3.txt 共7个部分,
并且将1.txt存入char * input_file中,将2.txt和3.txt存入char* output_file中
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-9-26 19:50:53 | 显示全部楼层
可以使用strtok函数将字符串分割成各个部分,具体实现如下:
char command[1024] = "cat<1.txt>2.txt>3.txt";
char* input_file;
char* output_file1;
char* output_file2;
char* token = strtok(command, "<>"); // 使用<和>作为分隔符
int count = 0;
while (token != NULL) {
    switch (count) {
        case 0:
            // 第一个部分为cat,可以忽略
            break;
        case 1:
            // 第二个部分为输入文件名
            input_file = token;
            break;
        case 2:
            // 第三个部分为输出文件名1
            output_file1 = token;
            break;
        case 3:
            // 第四个部分为输出文件名2
            output_file2 = token;
            break;
        default:
            // 多余的部分可以忽略
            break;
    }
    token = strtok(NULL, "<>"); // 继续寻找下一个分隔符
    count++;
}

这样就可以将字符串分割成7个部分,其中第1个部分为cat,可以忽略,第2个部分为输入文件名,第3和4个部分为输出文件名。将它们分别存入对应的变量中即可。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-9-26 21:45:27 | 显示全部楼层
本帖最后由 jackz007 于 2023-9-26 21:48 编辑
#include <stdio.h>
#include <string.h>

int main(void)
{
        char command[1024]="cat<1.txt>2.txt>3.txt" , input_file[1024] , output_file[2][1024] , component[10][1024]      ;
        int i , k , m                                                                                                   ;
        for(i = m = 0 ; command[i] ;) {
                for(k = 0 ; command[i] && command[i] != '<' && command[i] != '>' ; ) component[m][k ++] = command[i ++] ;
                component[m][k] = '\0'                                                                                  ;
                if(command[i]) {
                        component[m + 1][0] = command[i ++]                                                             ;
                        component[m + 1][1] = '\0'                                                                      ;
                        m += 2                                                                                          ;
                }
        }
        m ++                                                                                                            ;
        strcpy(input_file , component[2])                                                                               ;
        strcpy(output_file[0] , component[4])                                                                           ;
        strcpy(output_file[1] , component[6])                                                                           ;
        printf(" input_file is : %s\n" , input_file)                                                                    ;
        printf("output_file is : %s , %s\n" , output_file[0] , output_file[1])                                          ;
}
        编译、运行实况:
D:\[exercise]\C>g++ -o x x.c

D:\[exercise]\C>x
 input_file is : 1.txt
output_file is : 2.txt , 3.txt

D:\[exercise]\C>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 04:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表