C语言 C - 文件中的字符数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19416047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:43:42  来源:igfitidea点击:

C - Character Count from a File

cfilecountcharacter

提问by Sarah97

So I want to make a program which counts the occurrences of each character in a file. For example:

所以我想制作一个程序来计算文件中每个字符的出现次数。例如:

4 instances of character 0x67 (g)

字符 0x67 (g) 的 4 个实例

11 instances of character 0x68 (h)

字符 0x68 (h) 的 11 个实例

and so on

等等

I am not sure how to display and count instances.

我不确定如何显示和计算实例。

Any thoughts?

有什么想法吗?

#include <stdio.h>
const char FILE_NAME[] = "input.txt";
#include <stdlib.h>

int main() {

    int             count = 0;  /* number of characters seen */
    FILE           *in_file;    /* input file */

   /* character or EOF flag from input */
    int             ch;

    in_file = fopen(FILE_NAME, "r");
    if (in_file == NULL) {
        printf("Cannot open %s\n", FILE_NAME);
        exit(8);
    }

    while (1) {
        ch = fgetc(in_file);
        if (ch == EOF)
            break;
        ++count;
    }
    printf("Number of characters in %s is %d\n",
                  FILE_NAME, count);

    fclose(in_file);
    return (0);

回答by Escualo

This is what I came up with...

这是我想出来的……

#include<stdio.h>
#include<stdlib.h>

int main() {
  /* a buffer to hold the count of characters 0,...,256; it is
   * initialized to zero on every element */
  int count[256] = { 0 };

  /* loop counter */
  int k;

  /* file handle --- in this case I am parsing this source code */
  FILE *fp = fopen("ccount.c", "r");

  /* a holder for each character (stored as int) */
  int c;

  /* for as long as we can get characters... */
  while((c=fgetc(fp))) {

    /* break if end of file */
    if(c == EOF) break;

    /* otherwise add one to the count of that particular character */
    count[c]+=1;
  }

  /* now print the results; only if the count is different from
   * zero */
  for(k=0; k<256; k++) {
    if(count[k] > 0) {
      printf("char %c: %d times\n", k, count[k]);
    }
  }
  /* close the file */
  fclose(fp);

  /* that's it */
  return 0;
}

I compile the code using the following command (GCC 4.8.1 on OS X 10.7.4)

我使用以下命令编译代码(OS X 10.7.4 上的 GCC 4.8.1)

gcc ccount.c -Wall -Wextra -pedantic -ansi

it compiles with no warnings and no errors; this is the output:

它编译时没有警告和错误;这是输出:

char 
: 40 times
char  : 190 times
char ": 6 times
char #: 2 times
char %: 2 times
char ': 1 times
char (: 11 times
char ): 11 times
char *: 23 times
char +: 3 times
char ,: 5 times
char -: 3 times
char .: 9 times
char /: 20 times
char 0: 5 times
char 1: 1 times
char 2: 3 times
char 5: 3 times
char 6: 3 times
char :: 1 times
char ;: 13 times
char <: 3 times
char =: 7 times
char >: 3 times
char E: 2 times
char F: 2 times
char I: 2 times
char L: 1 times
char O: 1 times
char [: 4 times
char \: 1 times
char ]: 4 times
char a: 29 times
char b: 4 times
char c: 36 times
char d: 15 times
char e: 49 times
char f: 25 times
char g: 4 times
char h: 22 times
char i: 36 times
char k: 9 times
char l: 19 times
char m: 5 times
char n: 35 times
char o: 38 times
char p: 9 times
char r: 34 times
char s: 22 times
char t: 49 times
char u: 16 times
char v: 1 times
char w: 4 times
char y: 2 times
char z: 3 times
char {: 5 times
char }: 5 times

回答by I?ya Bursov

you need to use array, check out:

您需要使用数组,请查看:

int charArray[256];
memset(charArray, 0, 256*sizeof(int)); // instead of memset, for 0 values you can you just {0}

while (1) {
        ch = fgetc(in_file);
        if (ch == EOF)
            break;
        charArray[ch]++;
    }
for (int i=0; i<256; i++)
    if (charArray[i] > 0)
        printf("Number of character %c is %d\n", (char)i, charArray[i]);

回答by Ajeet Khan

A simple program I found here. It takes two input, first the character you want to count and the file name in which the occurrence of character has to be counted.

我在这里找到一个简单的程序。它需要两个输入,首先是您要计算的字符和必须计算字符出现次数的文件名。

回答by LihO

If you want to retrieve counts of alphabet characters, then it could look like this:

如果要检索字母字符的计数,则它可能如下所示:

int counts[26];
memset(&counts[0], 0, sizeof(counts));

while ( (ch = fgetc(in_file)) != EOF) {
    if (ch >= 'a' && ch <= 'z')
        ++count[ch - 'a'];
}

printing them out could be simple as:

打印出来可能很简单:

for (char c = 'a'; c <= 'z', ++c)
    printf("Count of '%c' is %d\n", c, count[c - 'a']);

回答by Joe DF

int strchro(char c, char *str) {

    char *pch;
    int found = 0;
    pch=strchr(str,c);
    while (pch!=NULL)
    {
        //printf("found at %d\n",pch-str+1);
        found++;
        pch=strchr(pch+1,c);
    }
    return found;
}

an old function i wrote a while ago.. hope this helps ;)
more info here: http://en.cppreference.com/w/c/string/byte/strchr

我前段时间写的一个旧函数..希望这有帮助;)
更多信息在这里:http: //en.cppreference.com/w/c/string/byte/strchr

回答by dreamlax

You could use an array of 256 integers (on many platforms charis an 8-bit value). Since the number of times a character appears in a file can't be negative, an unsigned type makes sense.

您可以使用 256 个整数的数组(在许多平台上char是 8 位值)。由于字符在文件中出现的次数不能为负数,因此无符号类型是有意义的。

unsigned charCount[256] = { 0 };

Each slot in the array represents the number of times the character with that value appears in that file.

数组中的每个槽表示具有该值的字符在该文件中出现的次数。

while ((ch = fgetc(in_file)) != EOF)
{
    // increment the count of character ch
    charCount[ch]++;
}

When printing them out, some characters are not printable or are whitespace (this is particularly applicable if you are reading a binary file), you can use the isprintand isspacefunctions found in the ctype.hheader.

打印出来时,有些字符是不可打印的或者是空白字符(这在读取二进制文件时特别适用),您可以使用标题中的isprintisspace函数ctype.h

for (int i = 0; i < 256; i++)
{
    // only display characters with a count of at least 1
    if (charCount[i] > 0)
    {
        if (!isprint(i) || isspace(i))
            printf("%u instances of character %x\n", charCount[i], (unsigned) i);
        else
            printf("%u instances of character '%c'\n", charCount[i], i);
    }   
}