|
发表于 2023-9-26 17:05:08
|
显示全部楼层
可以使用strchr函数来查找字符串中的特定字符,然后将字符串分成相应的部分。以下是一个可能的实现:
- fgets(command, sizeof(command), stdin);
- char *input_redirect = strchr(command, '<');
- char *output_redirect = strchr(command, '>');
- char *command_name = strtok(command, " \n");
- if (input_redirect) {
- *input_redirect++ = '\0';
- char *input_file = strtok(input_redirect, " \n");
- // do something with input_file
- }
- if (output_redirect) {
- *output_redirect++ = '\0';
- char *output_file = strtok(output_redirect, " \n");
- // do something with output_file
- }
- // do something with command_name
复制代码
这个实现首先使用strchr函数查找输入重定向符号“<”和输出重定向符号“>”。然后,它将命令字符串分成三个部分:命令名称、输入重定向文件名和输出重定向文件名。对于每个重定向文件名,它使用strtok函数将其分离出来。最后,您可以在代码中使用这些部分来执行相应的操作。 |
|