C語言 實現讀取檔案,並統計每個字元出現的個數
阿新 • • 發佈:2018-12-20
/***************** 實現讀取檔案,並統計每個字元出現的個數 *****************/ #include <stdio.h> #include <stdlib.h> unsigned long file_size; unsigned int frequency_count[512]; FILE* infilepointer; void get_frequency_count () { for (unsigned int i = 0; i < file_size; i++) { frequency_count[getc(infilepointer)]++; //getc (infilepointer)從檔案指標指向的檔案讀入一個字元 } } bool func(const char* infilename) { if(( infilepointer = fopen(infilename, "rb") ) != NULL) { fseek( infilepointer, 0L, 2);//將檔案位置指標指向檔案末尾 file_size = (unsigned long) ftell(infilepointer); fseek( infilepointer, 0L, 0); get_frequency_count (); fclose (infilepointer); return 1; } else { printf("Error! can't open %s",infilename); return 0; } } int main() { func("C:\\Users\\Pacer95\\Desktop\\12.txt"); printf("%d,",frequency_count[48]); }
12.txt裡的內容
統計出0字元的個數,如圖