Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 字符串搜索无法打印整行_C_Linux - Fatal编程技术网

C 字符串搜索无法打印整行

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

所以基本上我想做的是类似于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 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
可能永远不会成为真。