#include "iostream.h"
#include "stdlib.h"
#include "stdio.h"
#include "io.h"
class BinOutputer{
private:
FILE * outputFile;
char buffer;
int bit;
char getBitPo(int i){
if(i>7){
cout<<"Inner bit error!"<<endl;
exit(1);
}
int total=1;
for(int j=0;j<i;j++)
total*=2;
return total;
}
public:
BinOutputer(const char * fileName){
outputFile=NULL;
outputFile=fopen(fileName,"w+");
if(outputFile==NULL){
cout<<"Cannot open the output file."<<endl;
exit(1);
}
bit=7;
buffer=0;
}
FILE* getOutputFile(){
return outputFile;
}
void outputABit(int i){
cerr<<i;
switch(i){
case 1:
buffer+=getBitPo(bit);
case 0:
bit--;
if(bit==-1){
bit=7;
putc(buffer,outputFile);
cerr<<"\tBuffer output:"<<(long)buffer<<endl;
buffer=0;
}
break;
default:
cout<<"Bit output error!"<<endl;
exit(1);
}
}
void endOutput(){
if(bit!=7){
putc(buffer,outputFile);
cerr<<"Buffer output:"<<(long)buffer<<endl;
}
fclose(outputFile);
}
};
class BinInputer{
private:
FILE * inputFile;
char buffer;
int bit;
char getBitPo(int i){
if(i>7){
cout<<"Inner bit error!"<<endl;
exit(1);
}
int total=1;
for(int j=0;j<i;j++)
total*=2;
return total;
}
public:
BinInputer(const char * fileName){
inputFile=NULL;
inputFile=fopen(fileName,"r");
if(inputFile==NULL){
cout<<"Cannot open the output file."<<endl;
exit(1);
}
bit=-1;
buffer=0;
}
FILE* getInputFile(){
return inputFile;
}
int inputABit(){
if(bit==-1){
bit=7;
if((buffer=getc(inputFile))==EOF){
return -1;
}
}
int temp=getBitPo(bit);
bit--;
if((unsigned)buffer>=(unsigned)temp){
buffer=(char)((unsigned)buffer-(unsigned)temp);
//cerr<<'1';
return 1;
}
else{
//cerr<<'0';
return 0;
}
}
void endInput(){
fclose(inputFile);
}
};
posted on 2005-04-23 23:07
小爽 阅读(693)
评论(0) 编辑 收藏 所属分类:
我的经验