簡體   English   中英

掃描文件並計數c中的值時出現分段錯誤

[英]segmentation fault when scaning file and counting values in c

我正在掃描文件,並計算有多少個字母是大寫,小寫,數字或其他字符; 由於某種原因給了我細分錯誤; 我不太確定為什么這是我的代碼

#include<stdio.h>
#include<ctype.h>

int main(void)
{
    FILE *ifp, *ofp;
    char *mode = "r";
    char words;
    int lengthOfWords, i;
    int uppercase=0, lowercase=0, digits=0, other=0, total=0;

    ifp = fopen("story.txt", "r");

    if (ifp == NULL) {
        fprintf(stderr, "Can't open input file in.list!\n");
        return 0;
    }
    else
    {
        while(fscanf("%c", &words) != EOF)
        {
            if ((words>='A')&&(words<='Z'))
            {
                uppercase++;
            }
            else if ((words>='a')&&(words<='z'))
            {
                lowercase++;
            }
            else if ((words>='0')&&(words<='9'))
            {
                digits++;
            }
            else
            {
                other++;
            }
        }
    }
    printf("\n%d %d %d %d\n",uppercase, lowercase, digits, other );



return 0;

}

為什么我只是逐個字符地讀取它,並在運行時對其進行計數

順便說一下,這是我的txt文件。

The quick Fox jumps
over 2014 *$!&#@] lazy dogs.

您忘記傳遞FILE (流指針)作為fscanf函數的參數:

while (fscanf(ifp, "%c", &words) != EOF)

據該男子稱fscanf的簽名是:

int fscanf(FILE *restrict stream, const char *restrict format, ...);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM