Michael_19
Michael_19

Reputation: 4959

How can I count the occurrences of a character in a file?

The function is called as so,

printf("%d occurrences of %c in %s\n",
        countoccurrences(argv[1], argv[1][0]),
            argv[1][0], argv[1]);

and this is my function so far:

    /* 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;
}

When I run the program, ./main Today is a beutiful day

I get the error Segmentation fault (core dumped)

Upvotes: 1

Views: 3347

Answers (3)

pb2q
pb2q

Reputation: 59607

It looks like you're using your function countoccurrences in main before it's been defined.

Add a function signature before main:

int countoccurrences(char *, char);

Or move the function itself to a place in your code before your main function.

Also:

  • you need to initialize your count variable to zero in countoccurences, and
  • you should check that fp != NULL before you use the file pointer. fopen will return NULL if it can't open the file.

When I run the program, ./main Today is a beutiful day

When you run your program this way, you are passing it 5 arguments, one for each word in your sentence. Review your function and your function call in main: the function wants a file name to search, which should be the first argument to your program, not the text to search. And the second argument should be the character to search for.

Since you're not checking the return value of fopen, your invocation here will cause problems, because you probably don't have a file named Today in the working directory.

Upvotes: 3

hmjd
hmjd

Reputation: 121961

The error indicates that the function declaration or definition was not visible at the point where it is called. Move the definition or put a declaration prior to main().

Other points:

  • Check return value of fopen()
  • Initialise count
  • buf is an unused local variable

For example:

FILE *fp = fopen(filename,"r");
if (fp)
{
    int ch,count=0;
    while ((ch = fgetc(fp)) != EOF)
    {
        if (ch == c) count++;
    }
    fclose(fp);
}
else
{
    fprintf(stderr,
            "Failed to open %s: %s\n",
            filename,
            strerror(errno));
}

Upvotes: 2

CrazyCasta
CrazyCasta

Reputation: 28302

C needs to know about your function signature before the call. Either:

  1. Move your function before the call, or
  2. Put a declaration before the call (at the global scope of course)

    int countoccurrences(char *filename, char c);

You also need to initialize count (presumably to 0). You should make sure you are calling it with the right value. If you want to use the first character of the second parameter you should use argv[2][0].

Upvotes: 2

Related Questions