Hi all,
This is a script that reads in text from a file and separates it into characters. It works fine as long as MAXSTRLEN is under 610000.
However, I need it to read in whole books of text. Whenever I try to bump that number up above 610000 or so, I get a segmentation fault and I'm not sure why. Gdb is giving me this as an error:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400727 in main () at charfreq.c:19
19 for (i = 0;((ch=getchar()) != EOF) && (i < MAXSTRLEN); i++)
Any advice would be appreciated.
Thanks,
bucwet
#include <stdio.h>
#include <ctype.h>
#include <string.h>
//#define MAXSTRLEN 610000
#define CHARS 128
int main(void) {
long int const MAXSTRLEN = 1000000;
long int length,position,numArr[MAXSTRLEN],asciiArr[MAXSTRLEN],count,i;
int j;
char *str[MAXSTRLEN];
char ch;
count = 0;
// get source text
for (i = 0;((ch=getchar()) != EOF) && (i < MAXSTRLEN); i++)
{
str[i] = (int)ch;
count++;
}
length = strlen(str);
// print entered string
printf("Entered string is:\n");
for (i = 0; i < length; i++)
{
printf("%c",str[i]);
}
// convert string to ascii integer code
for (i = 0; i < length; i++)
{
numArr[i] = (long int)str[i];
}
printf("\n");
// assign asciiArr[] to zero
for (i = 0; i < CHARS; i++)
{
asciiArr[i] = 0;
}
// and go through the numArr, increment asciiArr[]
for (i = 0; i < length; i++)
{
position = numArr[i];
asciiArr[position]++;
}
// print out formatted text
for(j = 0; j < CHARS;j++)
{
if ((asciiArr[j] != 0) && (j != 10) && (j != 13))
{
printf("%4x: %4d: %3c %6ld %8f\n",j,j,j,asciiArr[j],(((float)asciiArr[j])/length));
}
else if (j == 10)
{
printf("%4x: %4d: \\n %6ld %8f\n",j,j,asciiArr[j],(((float)asciiArr[j])/length));
}
else if (j == 13)
{
printf("%4x: %4d: \\r %6ld %8f\n",j,j,asciiArr[j],(((float)asciiArr[j])/length));
}
}
printf("Amount of characters: %ld.\n",length);
printf("\n");
return(0);
}