How to read .txt files and read the strings in C language

如果在桌面中有一个huffman-coding.txt文件,其内容为:
In computer science and information theory, a Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression. The process of finding or using such a code proceeds by means of Huffman coding, an algorithm developed by David A. Huffman while he was a Sc.D. student at MIT, and published in the 1952 paper “A Method for the Construction of Minimum-Redundancy Codes”.
The output from Huffman’s algorithm can be viewed as a variable-length code table for encoding a source symbol (such as a character in a file). The algorithm derives this table from the estimated probability or frequency of occurrence (weight) for each possible value of the source symbol. As in other entropy encoding methods, more common symbols are generally represented using fewer bits than less common symbols. Huffman’s method can be efficiently implemented, finding a code in time linear to the number of input weights if these weights are sorted. However, although optimal among methods encoding symbols separately, Huffman coding is not always optimal among all compression methods - it is replaced with arithmetic coding or asymmetric numeral systems if better compression ratio is required.
How to read a string in a .txt file in C language, output it, and calculate the probability of each character.

#include <stdio.h>
#include<string.h>
int main()
{
    
    
	FILE*p;
	char ch;
	char str[10005];//用来存储txt文件中的字符串 
	int i,num[256]={
    
    0};
	if((p=fopen("huffman-coding.txt","r"))==NULL)  //以只读的方式打开test。
	{
    
    
		printf("ERROR");		
	}
	int k=0;
	for(;ch!='\n';)
	{
    
    
		ch=fgetc(p);   //ch得到p所指文件中的每一个字符
		str[k]=ch;
		k++;
		//putchar(ch);     //将得到的字符输出到屏幕
	}
	puts(str);
	for(i=0;i<strlen(str);i++)
    {
    
    
    	if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z'))
        	num[(int)str[i]]++;
        else 
        	continue;

	}
    for(i=0;i<256;i++)
        if(num[i]!=0)
            printf("字符%c出现%d次\n",(char)i,num[i]);
	fclose(p);	//关闭文件
}

The result is:
First output

Guess you like

Origin blog.csdn.net/weixin_43573126/article/details/105789224