鱼C论坛

 找回密码
 立即注册
查看: 1423|回复: 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中
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-26 19:50:53 | 显示全部楼层
可以使用strtok函数将字符串分割成各个部分,具体实现如下:

  1. char command[1024] = "cat<1.txt>2.txt>3.txt";
  2. char* input_file;
  3. char* output_file1;
  4. char* output_file2;
  5. char* token = strtok(command, "<>"); // 使用<和>作为分隔符
  6. int count = 0;
  7. while (token != NULL) {
  8.     switch (count) {
  9.         case 0:
  10.             // 第一个部分为cat,可以忽略
  11.             break;
  12.         case 1:
  13.             // 第二个部分为输入文件名
  14.             input_file = token;
  15.             break;
  16.         case 2:
  17.             // 第三个部分为输出文件名1
  18.             output_file1 = token;
  19.             break;
  20.         case 3:
  21.             // 第四个部分为输出文件名2
  22.             output_file2 = token;
  23.             break;
  24.         default:
  25.             // 多余的部分可以忽略
  26.             break;
  27.     }
  28.     token = strtok(NULL, "<>"); // 继续寻找下一个分隔符
  29.     count++;
  30. }
复制代码


这样就可以将字符串分割成7个部分,其中第1个部分为cat,可以忽略,第2个部分为输入文件名,第3和4个部分为输出文件名。将它们分别存入对应的变量中即可。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

  3. int main(void)
  4. {
  5.         char command[1024]="cat<1.txt>2.txt>3.txt" , input_file[1024] , output_file[2][1024] , component[10][1024]      ;
  6.         int i , k , m                                                                                                   ;
  7.         for(i = m = 0 ; command[i] ;) {
  8.                 for(k = 0 ; command[i] && command[i] != '<' && command[i] != '>' ; ) component[m][k ++] = command[i ++] ;
  9.                 component[m][k] = '\0'                                                                                  ;
  10.                 if(command[i]) {
  11.                         component[m + 1][0] = command[i ++]                                                             ;
  12.                         component[m + 1][1] = '\0'                                                                      ;
  13.                         m += 2                                                                                          ;
  14.                 }
  15.         }
  16.         m ++                                                                                                            ;
  17.         strcpy(input_file , component[2])                                                                               ;
  18.         strcpy(output_file[0] , component[4])                                                                           ;
  19.         strcpy(output_file[1] , component[6])                                                                           ;
  20.         printf(" input_file is : %s\n" , input_file)                                                                    ;
  21.         printf("output_file is : %s , %s\n" , output_file[0] , output_file[1])                                          ;
  22. }
复制代码

        编译、运行实况:
  1. D:\[exercise]\C>g++ -o x x.c

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

  5. D:\[exercise]\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 09:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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