C语言如何读取.txt文件并且读取其中的字符串

如果在桌面中有一个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.
如何在C语言中读取.txt文件中的字符串,并输出,并计算各个字符出现的概率。

#include 
#include
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);	//关闭文件
}

其结果为:
C语言如何读取.txt文件并且读取其中的字符串_第1张图片

你可能感兴趣的:(C语言)