读取文件为字节数组[已关闭]
发布时间:2020-12-16 03:27:46 所属栏目:百科 来源:网络整理
导读:我有一个编码霍夫曼算法的作业.我的头脑里有整个问题,但我在处理文件时遇到麻烦. 问题是:该算法应该压缩任何类型的文件. 我的解决方案:读取文件为字节数组,然后为每个字节使用一个int数组[256] = {0},得到相应的值,并增加数组[n].如果我没有说清楚,让我知
我有一个编码霍夫曼算法的作业.我的头脑里有整个问题,但我在处理文件时遇到麻烦.
问题是:该算法应该压缩任何类型的文件. 我的解决方案:读取文件为字节数组,然后为每个字节使用一个int数组[256] = {0},得到相应的值,并增加数组[n].如果我没有说清楚,让我知道. 所以,我做了很多研究,但是不了解如何从任何类型的文件中获取字节以及如何处理它们. 解决方法FILE *fileptr; char *buffer; long filelen; fileptr = fopen("myfile.txt","rb"); // Open the file in binary mode fseek(fileptr,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 + fread(buffer,filelen,1,fileptr); // Read in the entire file fclose(fileptr); // Close the file 现在你有一个包含文件内容的字节数组. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |