gpt4 book ai didi

你能帮我这个(C 语言)吗?

转载 作者:行者123 更新时间:2023-11-30 21:47:50 26 4
gpt4 key购买 nike

编写一个带有两个命令行参数的程序。第一个是字符串;第二个是文件名。然后程序应该搜索文件,打印包含该字符串的所有行。因为该任务是面向行的而不是面向字符的,所以使用 fgets() 而不是 getc()。使用标准 C 库函数 strstr() 在每一行中搜索字符串。假设没有更长的线超过 255 个字符。

最佳答案

/* with thanks to @AduaitPokhriyal */
#include <stdio.h>

int main(int argc, char *argv[])
{
char command[100]; /* i hope this is large enough! */
sprintf(command, "grep %s %s", argv[1], argv[2]); /* i hope the arguments are there and valid! */
system(command); /* surely "grep" must use strstr() somewhere */
return 0;
}

关于你能帮我这个(C 语言)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22533723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com