文件计数字符程序在C中(分段错误,未到达预期输出)

时间:2017-05-12 02:46:47

标签: c

#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;
}

这是我的预期输出: 编写一个程序来计算字符在文件中出现的次数。 (不区分大小写......'a'和'A'被认为是相同的)

示例输入和输出:

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

(假设服务器中有test.txt)**

我的输出:

*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)*                                                                                                                                                                              

1 个答案:

答案 0 :(得分:1)

首先,你的行:

scanf("%c", &character);
当用户点击进入时,

正在读取上一个scanf中的换行符。如果您想要输入除&#39; \ n&#39;之外的任何内容,这是有问题的。我将行改为:

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

至于您的分段错误,我只在输入未创建的文本文件时才会收到此错误。这会导致fopen返回null。当你传递foef()一个空指针时,你会得到一个分段错误。确保输入已创建的文本文件。要解决这两个问题,请尝试以下方法:

#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;
}