Hi,
I´ve written this program to input some data, and make some calculation on this data. Problem is that I cannot get the input working. I´ve copied the code from another program of mine where there are no problems. I´m using DJGPP, and I´ve tried to compile it on Solaris too. On Solaris it works fine - THE SAME CODE!!1
Can anyone help me explain this??
Code is as goes:
Code:
#include <math.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
using namespace std;
void calcProbs(unsigned char* data ,int* probs,int size);
double getEntropy(int* probs,int size);
void makeDiffs(unsigned char* data,int size);
#define MAXFILESIZE 1000000
int main(int argc,char** argv){
unsigned char* data; // Data buffer
unsigned char* data2; // Data buffer
int i,j; // Loop vars
int probs[256]; // Probability vars
double entropy; // Calculated Entropy
ifstream in; // Input Filestream
char* temp; // Temporary var
if(argc!=2){ cerr << "Wrong number of arguments" << endl; return 0;}
data=(unsigned char*) malloc(MAXFILESIZE); // Alloc some memory
if(data==NULL){ return 0; }
data2=(unsigned char*) malloc(MAXFILESIZE); // Alloc some memory
if(data2==NULL){ free(data); return 0; }
i=0;
in.open(argv[1],ifstream::in); // open file
while(in.good()){
data[i++]=in.get();
}
in.close(); // close file
// Read input!
cout << i << endl; // Output numbers of bytes read
calcProbs(data,probs,i);
entropy=getEntropy(probs,i);
cout << "original entropy: " << entropy << endl;
makeDiffs(data,i);
calcProbs(data,probs,i);
entropy=getEntropy(probs,i);
cout << "difference entropy: " << entropy << endl;
free(data);
free(data2);
}
void calcProbs(unsigned char* data ,int* probs,int size){
int i;
unsigned char c;
for(i=0;i<256;i++){
probs[i]=0;
}
for(i=0;i<size;i++){
c=data[i];
probs[c]++;
}
}
double getEntropy(int* probs,int size){
int i;
double size2,temp,temp2;
temp2=0.0;
size2=(double) size;
for(i=0;i<256;i++){
if(probs[i]){
temp=(double) probs[i];
temp/=size2;
temp2+=temp*log(temp);
}
}
temp2/=log(2.0);
temp2=-temp2;
return temp2;
}
void makeDiffs(unsigned char* data,int size){
unsigned char c,d;
int i;
c=0;
for(i=0;i<size;i++){
d=data[i];
data[i]=d-c;
c=d;
}
}