Muthu Raman M
Muthu Raman M

Reputation: 7

File count character program In C (Segmentation fault and not arriving expected output)

#include<stdio.h>
#include<stdlib.h>
#define len 20
int main (void)
{
    FILE *fp;
    char filename[len];
    char character,temp;
    int count = 0;
    printf("Enter the file name\n");
    scanf("%s",filename);
    printf("Enter the character to be counted\n");
    scanf("%c",&character);
    fp=fopen(filename,"r");
    while(!feof(fp))
    {
        temp=fgetc(fp);
        if(temp==character)
         count++;
    }
    printf("File '%s' has %d instances of letter '%c'", filename, count, character);
    fclose(fp);
    return 0;
}

This is my expected output: Write a program to count the number of times a character appears in the File. (Case insensitive... 'a' and 'A' are considered to be the same)

Sample Input and Output:

Enter the file name
test.txt
Enter the character to be counted
r
File 'test.txt' has 99 instances of letter 'r'.

(assuming test.txt in server)**

My output:

*sh-4.2$ gcc -o main *.c                                                                                                                                                                                       
sh-4.2$ gcc -o main *.c                                                                                                                                                                                       
sh-4.2$ main                                                                                                                                                                                                  
Enter the file name                                                                                                                                                                                           
test.txt                                                                                                                                                                                                      
Enter the character to be counted                                                                                                                                                                             
Segmentation fault (core dumped)                                                                                                                                                                              
sh-4.2$ main                                                                                                                                                                                                  
Enter the file name                                                                                                                                                                                           
test.txt                                                                                                                                                                                                      
Enter the character to be counted                                                                                                                                                                             
Segmentation fault (core dumped)*                                                                                                                                                                              

Upvotes: 0

Views: 48

Answers (1)

Jayson Boubin
Jayson Boubin

Reputation: 1504

First, your line:

scanf("%c", &character);

is reading the newline character from your previous scanf when the user hits enter. This is problematic if you want to enter anything besides '\n'. I changed the line to :

do {
    scanf("%c", &character);
} while(character == '\n');

As for your segmentation fault, I only receive this error when I input a text file which isn't created. This causes fopen to return null. When you pass foef() a null pointer, you get a segmentation fault. Make sure you are entering a text file which has been created. To remove both of these problems, try this:

#include<stdio.h>
#include<stdlib.h>
#define len 20
int main (void)
{
    FILE *fp;
    char filename[len];
    char character,temp;
    int count = 0;
    printf("Enter the file name\n");
    scanf("%s",filename);
    printf("Enter the character to be counted\n");
    do {
        scanf("%c", &character);
    } while(character == '\n');

    fp=fopen(filename,"r");
    if(fp != NULL) {
        while(!feof(fp))
        {
            temp=fgetc(fp);
            if(temp==character)
                count++;
        }
        printf("File '%s' has %d instances of letter '%c'", filename, count, character);
        fclose(fp);
    }
    else
        printf("File Entered Does Not Exist");
    return 0;
}

Upvotes: 1

Related Questions