C 字符串搜索无法打印整行
所以基本上我想做的是类似于grep的事情 ./a.out文件字符串 这是我的密码C 字符串搜索无法打印整行,c,linux,C,Linux,所以基本上我想做的是类似于grep的事情 ./a.out文件字符串 这是我的密码 fp = fopen(file, "r"); while(fgets(buffer, 200, fp) != NULL ) { line++; //Add new line if(strstr(pattern, buffer) != NULL) { sprintf(data, "%s", buffer); //So we can
fp = fopen(file, "r");
while(fgets(buffer, 200, fp) != NULL )
{
line++; //Add new line
if(strstr(pattern, buffer) != NULL)
{
sprintf(data, "%s", buffer); //So we can tell if the file is clean
printf("Wow what do we have here?\n Data:%s\nLine:%d\n", data, line);
}
else if(data == NULL)
{
printf("Looks like you are clean :/ or maybe i just suck\n");
}
}
现在,这将找到字符串,但它必须位于行的开头,例如
cat example.txt
第#行,这将被找到
我的目标是让它在行中找到字符串并打印整行strstr函数的定义是:
char *strstr(const char *haystack, const char *needle);
要查找的“模式”应该是第二个参数,“缓冲区”应该是第一个参数
注意:参考手册页是一个非常重要的习惯 strstr功能的定义是:
char *strstr(const char *haystack, const char *needle);
要查找的“模式”应该是第二个参数,“缓冲区”应该是第一个参数
注意:参考手册页是一个非常重要的习惯
strstr(模式,缓冲区)
-->strstr(缓冲区,模式)
还有data==NULL
可能永远不会成为真。strstrstr(模式,缓冲区)
-->strstr(缓冲区,模式)
还有data==NULL
可能永远不会成为真。