M
Martin Hvidberg
Please refere to the source below:
The program is supposed to read s ascii text file and count the number of
occurences of each of the 256 ascii codes. The result is presented on
screen.
It seems that it counts all charecters as ascii=1
What is wrong --- please.
---8<---
/* Return codes
0: Succes
1: command line parameters missing
2: Error opening In file
3: Error opening Out file */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *pInFile;
int i, alph[256], a;
char c;
if (argc!=2)
{
printf("\n Usage: %s <Infile>\n",argv[0]);
return 1;
}
else
{
if ((pInFile = fopen(argv[1],"r"))==NULL)
{
printf("\n\n ERROR - Can't open InFile %s\n\n",argv[1]);
return 2;
}
else printf("> InFile %s open ...\n",argv[1]);
}
for (i=0;i<=255;i++)
{
alph=0;
}
i=0;
while(c=fgetc(pInFile)!=EOF)
{
i++;
a = c; // c as integer
alph[a]++;
}
printf("\n Found %d chars.",i);
for (i=0;i<=255;i++) printf("\n %d:\t%d",i,alph);
fclose(pInFile);
return 0;
}
The program is supposed to read s ascii text file and count the number of
occurences of each of the 256 ascii codes. The result is presented on
screen.
It seems that it counts all charecters as ascii=1
What is wrong --- please.
---8<---
/* Return codes
0: Succes
1: command line parameters missing
2: Error opening In file
3: Error opening Out file */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *pInFile;
int i, alph[256], a;
char c;
if (argc!=2)
{
printf("\n Usage: %s <Infile>\n",argv[0]);
return 1;
}
else
{
if ((pInFile = fopen(argv[1],"r"))==NULL)
{
printf("\n\n ERROR - Can't open InFile %s\n\n",argv[1]);
return 2;
}
else printf("> InFile %s open ...\n",argv[1]);
}
for (i=0;i<=255;i++)
{
alph=0;
}
i=0;
while(c=fgetc(pInFile)!=EOF)
{
i++;
a = c; // c as integer
alph[a]++;
}
printf("\n Found %d chars.",i);
for (i=0;i<=255;i++) printf("\n %d:\t%d",i,alph);
fclose(pInFile);
return 0;
}