structure woes equals undesired output
okay... i'm having a little trouble with the output i'm receiving. in the "context" column (read comment header below) at the end of each line are some control characters (ASCII values 01, 02, and 03).
you'll really have to compile the code and review the output to understand. and if anyone can add any improvements, that would be great too.
i've been fumbling with this for two days with no luck. thanks.
Code:
/*
* FREQ.C -- blah blah
*
* given input of "this_is_a_test" freq.c will output to a file
*
* char freq context
* ---- ---- -------
* _ 3 a, i, t
* a 1 _
* e 1 s
* i 2 s
* h 1 i
* s 3 _, t
* t 3 e, h
*
* the "context" column represents characters that appear to the right
* of each character processed.
*
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE 256
struct data {
unsigned char achar;
unsigned char context[TABLE_SIZE-1];
unsigned long int count;
};
struct data symbol[TABLE_SIZE-1];
void init_symbol_array();
void get_stats(char *);
void print_stats(char *);
int main(int, char *[]);
/*
* Kinda obvious, but zero out array
*/
void
init_symbol_array(void)
{
int i, j;
for (i = 0; i < TABLE_SIZE; i++) {
symbol[i].achar = i;
symbol[i].count = 0;
for (j = 0; j < TABLE_SIZE; j++)
symbol[i].context[j] = 0;
}
}
/*
* Get frequency count of each character
*/
void
get_stats(char *file)
{
int c, i;
unsigned char buf[8192];
FILE *fp;
if((fp = fopen(file, "rb")) == NULL) {
(void)fprintf(stderr, "\nerror: %s: Can't open '%s'\n",
strerror(ENOENT), file);
exit(EXIT_FAILURE);
}
while ((c = fread(buf, 1, 8192, fp)) > 0) {
for (i = 0; i < c; i++) {
symbol[(int)buf[i]].count++;
symbol[(int)buf[i]].context[(int)buf[i+1]] = (int)buf[i+1];
}
}
fclose(fp);
}
/*
* Print symbol stats to output file
*/
void
print_stats(char *file)
{
FILE *fp;
int i, j;
if((fp = fopen(file, "w")) == NULL) {
(void)fprintf(stderr, "\nerror: %s: Can't write to '%s'\n",
strerror(ENOENT), file);
exit(EXIT_FAILURE);
}
fprintf(fp, "char\tfreq\tcontext\n");
fprintf(fp, "----\t----\t-------");
for (i = 0; i < TABLE_SIZE; i++) {
if (symbol[i].count > 0) {
fprintf(fp, "\n%c\t%lu\t", symbol[i].achar, symbol[i].count);
for (j = 0; j < TABLE_SIZE; j++) {
if (symbol[i].context[j] > 0)
fprintf(fp, "%c", symbol[i].context[j]);
}
}
}
fclose(fp);
}
/*
* This is where it all happens; the main function.
*/
int
main(int argc, char *argv[])
{
if(argv[2] == NULL)
argv[2] = "output.txt";
init_symbol_array();
printf("\nGathering statistics... ");
get_stats(argv[1]);
printf("done\n");
print_stats(argv[2]);
exit(EXIT_SUCCESS);
}