Ç - 字符从文件数 [英] C - Character Count from a File

查看:103
本文介绍了Ç - 字符从文件数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想打一个程序,计算每个字符出现在文件中。例如:

字符0x67(克)4个实例

字符为0x68的11实例(H)

我不知道如何显示和计算实例。

有什么想法?

 的#include<&stdio.h中GT;
为const char FILE_NAME [] =input.txt的;
#包括LT&;&stdlib.h中GT;诠释主(){    诠释计数= 0; / *看到的字符数* /
    FILE * in_file中; / *输入文件* /   / *从输入字符或EOF标志* /
    INT CH;    in_file中FOPEN =(FILE_NAME,R);
    如果(in_file中== NULL){
        的printf(无法打开%s \\ n,FILE_NAME);
        出口(8);
    }    而(1){
        CH =龟etc(in_file中);
        如果(CH == EOF)
            打破;
        ++计数;
    }
    的printf(%s中的字符数为%d \\ n,
                  FILE_NAME,计数);    FCLOSE(in_file中);
    返回(0);


解决方案

这是我想出了...

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;诠释主(){
  / *缓冲器来保存字符0的计数,...,256;它是
   *每个元素初始化为零* /
  诠释计数[256] = {0};  / *循环计数器* /
  时int k;  / *文件句柄---在这种情况下我解析这个源$ C ​​$ C * /
  FILE *计划生育=的fopen(ccount.c,R);  / *为每个字符(存储为int)持有人* /
  INT℃;  / *,只要我们能得到的字符... * /
  而((C =龟etc(FP))){    / *如果突破文件结束* /
    如果(C == EOF)打破;    / *否则添加一个到特定字符的个数* /
    算上[C] + = 1;
  }  / *现在打印结果;仅当计数为从不同
   *零* /
  对于(K = 0; K< 256; k ++){
    如果(计数[k]的大于0){
      的printf(CHAR%C:%d次的\\ n,K,计数[K]);
    }
  }
  / *关闭文件* /
  FCLOSE(FP);  /* 而已 */
  返回0;
}

我使用下面的命令编译code(GCC 4.8.1在OS X 10.7.4)

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

它没有警告,并没有错误编译;这是输出:

 字符
:40倍
焦炭:190次
字符:6次
字符#:2次
CHAR%:2次
符':1次
CHAR(11次
字符):11次
字符*:23次
字符+:3次
CHAR,5次
CHAR - :3次
焦炭:9次
字符/:20次
烧焦0:5次
CHAR 1:1次
烧焦2:3次
烧焦5:3次
烧焦6:3次
烧焦:: 1次
字符;:13次
焦炭<:3次
字符=:7次
炭计算值:3次
CHAR E:2次
CHAR F:2次
烧焦我:2次
CHAR L:1次
CHAR○:1次
CHAR [:4次
字符\\:1次
字符]:4倍
所以char a:29次
CHAR乙:4次
字符C:36次
CHAR D:15次
CHAR E:49次
CHAR F:25次
CHAR G:4次
字符H:22次
烧焦我:36次
CHAR K:9倍
CHAR L:19次
CHAR L:5次
CHAR N:35次
CHAR○:38次
CHAR号码:9次
字符R:34次
char中:22次
烧焦T:49次
烧焦U:16次
CHAR五:1次
字符W:4次
烧焦Y:2次
CHAR Z:3次
CHAR {:5次
字符}:5次

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)

11 instances of character 0x68 (h)

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

解决方案

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)

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

这篇关于Ç - 字符从文件数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆