我怎么把这个程序中生成的lzw文件啊

qq_38423382 2017-06-03 04:55:22
就是我想把lzw以二进制形式打开再将他的0和1依次读入string类型中,我该怎么弄。
就是这些代码生成的那个文件。
#include"LZW.h"
void LZW::ParaConfiguration() //参数初始化
{
half_flags = true;
code[0] = 0;
code[1] = 0;
code_index = 0;
}
void LZW::ReconstrucionDict() //字典表初始化
{
dict_index = 258;
for (int i = 0; i < 256; i++) //字典项前256项存放ACSII码表
{
dict[i].pre_code = -1;
dict[i].character = i;
}
for (int i = 256; i < 4096; i++) //字典项257—4096项存放LZW编码表
{
dict[i].pre_code = -1;
dict[i].character = NULL;
}
}
int LZW::Locate(int s, char c)
{
for (int i = 0; i < dict_index; i++)
{
if ((dict[i].pre_code == s) && (dict[i].character == c)) //搜索前项为s,本项为c的表项,并返回其位置
{
return i;
}
}
return -1;
}
void LZW::Push(ofstream& of, int code)
{
if (half_flags)
{
middle = 0;//使用之前要清零,防止上次残留的信息影响下次运算
middle = (char)((code & 0x0000000f) << 4); //位运算
low = (char)(code >> 4);
half_flags ^= 1;
}
else
{
high = (char)code;
middle |= (char)((code & 0x00000f00) >> 8);
of << low << middle << high;
half_flags ^= 1;
}
}
void LZW::EnCode()
{
ReconstrucionDict();
ParaConfiguration();
ifstream fin("source.txt");
ofstream fout("encode.lzw", ios::binary);//二进制文件输出
char Character = 0;
int Location = -1;
int PreCode = fin.get();
while (!fin.eof())
{
Character = fin.get();
Location = Locate(PreCode, Character);
if (Location != -1)
{
PreCode = Location;
}
else
{
Push(fout, PreCode);
dict[dict_index].pre_code = PreCode;
dict[dict_index].character = Character;
dict_index++;
if (dict_index == 4096) //编码表到达4096项,设置clear位
{
Push(fout, 256);
ReconstrucionDict();
PreCode = Character;
Character = 0;
Location = 0;
continue;
}
PreCode = Character;
}
}
if (!half_flags) //填充结束字符
{
Push(fout, 257); Push(fout, 257);
}
else
Push(fout, 257);
fin.close();
}
void LZW::InputCode(ifstream& fin)
{
code[0] = 0;
code[1] = 0;
low = fin.get();
middle = fin.get();
high = fin.get();
int Temp = 0;
Temp = (int)low;
code[0] = Temp << 4;
Temp = (int)middle;
code[0] |= (Temp & 0x000000f0) >> 4;
code[1] |= (Temp & 0x0000000f) << 8;
Temp = (int)high;
code[1] |= Temp;
}
void LZW::OutPutString(ofstream& fout, ENTRY Entry)
{
if (Entry.pre_code < 258)
{
if (Entry.pre_code != 1)
{
fout << (char)Entry.pre_code;
}
}
else
{
OutPutString(fout, dict[Entry.pre_code]);
}
fout << Entry.character;
}
char LZW::GetFirstCharOfPreCode(ENTRY Entry)
{
char a = 0;
if (Entry.pre_code == -1)
a = Entry.character;
else
{
if (Entry.pre_code < 258)
a = (char)Entry.pre_code;
else
a = GetFirstCharOfPreCode(dict[Entry.pre_code]);
}
return a;
}
void LZW::DeCode()
{
ReconstrucionDict();
ParaConfiguration();
ifstream fin("encode.lzw", ios::binary);
ofstream fout("decode.txt");
int PreCode = -1;
int CrtCode = 0;
ENTRY Entry;
InputCode(fin);
while (!fin.eof())
{
if (code[code_index] == 257) break;
if (code[code_index] == 256)
{
ReconstrucionDict();
PreCode = -1;
CrtCode = 0;
code_index++;
if (code_index == 2)
{
code_index = 0; InputCode(fin);
}
continue;
}
CrtCode = code[code_index];
code_index++;
if (code_index == 2)
{
code_index = 0; InputCode(fin);
}
Entry = dict[CrtCode];
if (Entry.character == NULL)
{
Entry.pre_code = PreCode;
Entry.character = GetFirstCharOfPreCode(dict[PreCode]);
}
OutPutString(fout, Entry);
if (PreCode != -1)
{
dict[dict_index].pre_code = PreCode;
dict[dict_index].character = GetFirstCharOfPreCode(Entry);
dict_index++;
}
PreCode = CrtCode;
}
fin.close();
}
...全文
140 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-06-05
  • 打赏
  • 举报
回复
fwrite

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧