ali*_*rda 20 c file-io huffman-code
我有一个编码霍夫曼算法的任务.我把整个问题整理在脑海中,但我在文件处理方面遇到了一些麻烦.
问题是:该算法应该压缩任何类型的文件.
我的解决方案:将文件读取为字节数组,然后使用int array[256]={0}
每个字节,获取int n
相应的值并递增array[n]
.如果我没说清楚,请告诉我.
所以,我做了很多研究,但不明白如何从任何类型的文件中获取字节以及如何处理它们.
use*_*193 41
FILE *fileptr;
char *buffer;
long filelen;
fileptr = fopen("myfile.txt", "rb"); // Open the file in binary mode
fseek(fileptr, 0, SEEK_END); // Jump to the end of the file
filelen = ftell(fileptr); // Get the current byte offset in the file
rewind(fileptr); // Jump back to the beginning of the file
buffer = (char *)malloc((filelen+1)*sizeof(char)); // Enough memory for file + \0
fread(buffer, filelen, 1, fileptr); // Read in the entire file
fclose(fileptr); // Close the file
Run Code Online (Sandbox Code Playgroud)
现在你有一个包含文件内容的字节数组.
归档时间: |
|
查看次数: |
60407 次 |
最近记录: |