Hello,
It's been almost new years eve, so first let me wish anyone a Happy New Year full of happiness and health.
Now, to the issue at hand. Consider the following simple source code
#include <stdio.h>
#define MAXCHARS 127
void make_probfile(FILE *infile, FILE *outfile)
{
unsigned int cfreq[MAXCHARS];
unsigned int numchrs=0;
int ch;
for(int iCount=0; iCount<=MAXCHARS; iCount++)
cfreq[iCount]=0;
while((ch=getc(infile)) != EOF)
{
if(ch >= 0 && ch <=MAXCHARS)
{
cfreq[ch]++;
numchrs++;
}
}
fclose(infile);
for(int iCount=0; iCount<=MAXCHARS; iCount++)
fprintf(outfile,"%.8f\n", (float)((float)cfreq[iCount] / (float)numchrs));
//fprintf(outfile,"%i: cfreq[%c]=%i, numchrs=%i : %.8f\n", iCount, iCount, cfreq[iCount], numchrs, ((float)cfreq[iCount] / (float)numchrs));
fclose(outfile);
}
void main()
{
FILE *fp1, *fp2;
fp1=fopen("c:\\temp\\sample.txt","r");
fp2=fopen("c:\\temp\\outfile.txt","w");
make_probfile(fp1,fp2);
}
We supply one big ASCII text file "sample.txt" to the program (inside c:\temp), that contains only characters from the first 128 (0-127) ASCII character set. The program calculates the frequency that each character appears on the text and then each character's probability. The results are stored on the c:\temp\outfile.txt file.
The line
fprintf(outfile,"%.8f\n", (float)((float)cfreq[iCount] / (float)numchrs));
produces different results on the last character (character 127), regarding the computed probability of this character, than the line
fprintf(outfile,"%i: cfreq[%c]=%i, numchrs=%i : %.8f\n", iCount, iCount, cfreq[iCount], numchrs, ((float)cfreq[iCount] / (float)numchrs));
The code works fine on Visual C++ and MinGW32 under Windows, so I suspect that there is something that either I am doing wrong with PellesC or there is a bug in fprintf().
Any ideas what's going wrong with this code?
Many thanks in advance.