Thread: Input problems with DJGPP?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    Input problems with DJGPP?

    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;
    	}	
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Problem is that I cannot get the input working
    Care to be a bit explanatory than "it doesn't work" ?

    >>while(in.good()){
    >>data[i++]=in.get(); }
    This is a buffer overflow just waiting to happen.

    [edit]
    >>in.open(argv[1],ifstream::in);
    Check the file open worked OK.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    >>>while(in.good()){
    >>>data[i++]=in.get(); }
    >This is a buffer overflow just waiting to happen.

    I know that, but right now it´s only in test phase, I´ll adjust that later.

    >>>in.open(argv[1],ifstream::in);
    >Check the file open worked OK.

    Ok, I should do that, but thats not what´s causing my problem.

    Any suggestions??

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Any suggestions??
    Yes, for the second time:
    >>Care to be a bit more explanatory than "it doesn't work" ?

    I compiled/run it, and it said:
    Code:
    4764
    original entropy: 4.26253
    difference entropy: 5.07116
    I've got no idea what it is supposed to do, or what it is doing wrong. So why don't you tell us, then we can help you better.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    Sure! It´s a program which calculates the entropy of a source. The entropy is a statistic measure, and the calculations are working like they should. The problem is, that when run on window - compiled with DJGPP, it doesn´t load all the file I specify. You´ll see, first it outputs how many bytes read, which should equal the size of the file specified when runned. Under Windows - compiled with DJGPP, it just don´t load the hole file. More strange is, that when runned on Solaris - compiled with GCC - no problems. I´d like to here some suggestions as it appear strange to me...

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> it just don´t load the hole file
    Do you mean it's a few bytes short? If so, it's because the file is being opened in text mode, and the CR LF bytes that mark and end of line are combined into one \n in your program. On *nix, an end of line marker is LF only, so there is a one to one relationship.

    If you want to get the "exact" data into your program, open with binary mode (ios::binary). This will then work on both *nix and Windows.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    Should that be like:

    Code:
    in.open(argv[1],ifstream::in|ios::binary);
    Or how should I specify it?

    Another things is - it´s not just a few bytes short, but it depends much on the file specified. You can have a look yourself!

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    That should do.

    >>You can have a look yourself!
    I did There's still a problem though, you might find it's one byte out. If you can't work that one out, ask again...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    That helped! But - yeah - I´m getting a byte too much... Do you know why, cause I can´t understand this one as well....

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Make your read loop like this:
    Code:
    int ch;
    while((ch = in.get()) != EOF){
      data[i++]=ch;
    }
    This explains the problem, but in C terms.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    29
    Thanks! That solved all my problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  3. Problems with input and fflush
    By edugarcia in forum Linux Programming
    Replies: 1
    Last Post: 11-24-2004, 01:52 PM
  4. Problems with commas as input
    By Yojimbo III in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2003, 09:18 PM
  5. White space problems in file input
    By cxs00u in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2002, 11:06 PM