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
linux上C中命令行上的多个搜索字符串存在问题_C_Linux - Fatal编程技术网

linux上C中命令行上的多个搜索字符串存在问题

linux上C中命令行上的多个搜索字符串存在问题,c,linux,C,Linux,我试图通过命令行将多个参数传递到linux终端上的C程序中,但遇到了一个分段错误 这是我的输出: $ ./a.out bar a a you made it past opening and reading file your file size is 7 Your count is:2 Segmentation fault (core dumped) 这是我应该得到的: $ ./a.out bar a a you made it past opening and reading file

我试图通过命令行将多个参数传递到linux终端上的C程序中,但遇到了一个分段错误

这是我的输出:

$ ./a.out bar a a
you made it past opening and reading file
your file size is 7
Your count is:2
Segmentation fault (core dumped)
这是我应该得到的:

 $ ./a.out bar a a
 you made it past opening and reading file
 your file size is 7
 Your count is:2
 Your count is:2
这是我的密码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[] ) 
{

  /******* Open, Read, Close file**********************************/

  FILE *ReadFile;

  ReadFile = fopen(argv[1], "r");

  if(NULL == ReadFile)
    {
      printf("\n file did not open \n");
      return 1;
    }

  fseek(ReadFile, 0 , SEEK_END);      
  int size = ftell(ReadFile);         
  rewind(ReadFile);                   

  char *content = calloc( size +1, 1); 

  fread(content,1,size,ReadFile);     

  /*fclose(ReadFile); */              

  printf("you made it past opening and reading file\n");
  printf("your file size is %i\n",size);

  /******************************************************************/


  /*********************String compare and print*********************/
  int count =0;
  int inputs;

  for( inputs = 2; inputs < argc ; inputs++)
    { 
  char *input = argv[inputs];


  while (content = strstr(content,"a"))
    {
      count++;
      content++;
  }
  printf("Your count is:%i\n",count);

  }
  /*****************************************************************/
    return 0;
}
很可能返回
NULL
,因为它找不到更多的
a
。当您再次尝试
strstr
时,它会尝试搜索NULL,这会导致分段错误

也许你想这么做

content = strstr(input, "a");

在while循环或类似的东西之前

更新:

不知怎的,我完全错过了,
bar
是一个文件名。因此,如果要在该文件中搜索多个字符串,只需让
content
单独使用一个不同的变量进行搜索,并在每次搜索另一个字符串时重置该变量

for (inputs = 2; inputs < argc; inputs++) { 
    char *input = argv[inputs];
    char *search = content;

    while (search = strstr(search, input)) {
        count++;
        search++;
    }

    printf("Your count is:%i\n", count);
}
for(输入=2;输入
来自原始Linux手册页来源:您的代码还需要重置每个循环的计数,除非计数为total@user1281385事实上,我不知道OP的意图是什么。但是感谢您的关注,我没有想到这一点;运行
/mytest
并用
gdb
调试它,我尝试过,但在while循环中得到了一个警告:“在赋值周围建议括号用作真值[-wparenthes]”,当我尝试/mytest时,文件没有打开,直到没有收到警告为止。然后在您正在运行的命令中将
/a.out
替换为
/mytest
,例如运行
/mytest bar a
,甚至学习如何使用
gdb
(从
gdb./mytest
开始)了解如何修复警告:“警告:建议在用作真值的赋值周围加括号[-wparenthes]我认为这是指while语句lineCode
while((content=strstrstr(content,input)))
while((content=strstrstr(content,input)))!=空)
content = strstr(input, "a");
content = input;
for (inputs = 2; inputs < argc; inputs++) { 
    char *input = argv[inputs];
    char *search = content;

    while (search = strstr(search, input)) {
        count++;
        search++;
    }

    printf("Your count is:%i\n", count);
}