【发布时间】:2019-07-08 21:29:37
【问题描述】:
我正在尝试实现重定向。我有来自用户的输入,我正在尝试从中提取输出文件。我正在使用 strstr() 来查找“>”的第一次出现。从那里我可以提取字符串的其余部分,但我不确定如何完成此操作。
我曾尝试将 strstr() 与 strcpy() 一起使用,但没有成功。
// char_position is the pointer to the character '>'
// output_file is the file that I need to extract
// line is the original string
// example of input: ls -l > test.txt
char *chr_position = strstr(line, ">");
char *output_file = (char *) malloc(sizeof(char) * (strlen(line) + 1));
strcpy(output_file + (chr_position - line), chr_position // something here?);
printf("The file is %s\n", output_file);
预期结果是从 > 到行尾构建一个字符串。
【问题讨论】: