该函数是这样调用的,
printf("%d occurrences of %c in %s\n",
countoccurrences(argv[1], argv[1][0]),
argv[1][0], argv[1]);
到目前为止,这是我的功能:
/* countcharinfile
* input: char *filename, char c
* output: the number of occurrences of char c inside file filename
*/
int countoccurrences(char *filename, char c)
{
// count the number of occurrences of c in the file named filename
FILE *fp = fopen(filename,"r");
int ch,count=0;
while ((ch = fgetc(fp) != EOF))
{
if (ch == c)
count++;
}
return count;
}
当我运行程序时,./main Today is a beutiful day
我得到错误Segmentation fault (core dumped)