I have a program I'm working on that will read a txt file and count the unique words and how many times that word appears.
I'm getting an error when I try to run it that says "Debug Error! Run-Time Check Failure #3 - The variable 'last' is being used without being initialized."
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
/*
Custom list data type
*/
typedef struct list_s {
struct list_s *prev, *next;
char *text;
int count;
} list_t;
/*
Add text to the list.
Returns the list on sucess.
Returns NULL on addition failures,
i.e. the text already there or no memory available.
*/
static list_t *list_add(list_t *list, char *text)
{
list_t *last, *node, *n;
for (n = list; n != NULL; n = n->next) {
if (!stricmp(n->text, text)) {
n->count++;
return NULL;
}
last = n;
}
if (!(node = malloc(sizeof(list_t)))) {
return NULL;
}
node->text = strdup(text);
node->count = 1;
node->next = NULL;
if (last == NULL) {
node->prev = NULL;
return node;
}
node->prev = last;
last->next = node;
return list;
}
/*
free the whole list and its associated data
*/
static void list_free(list_t *list)
{
list_t *node = list;
while (node != NULL) {
list_t *next = node->next;
if (node->text)
free(node->text);
free(node);
node = next;
}
}
/*
simple print of word list
*/
static void list_print(list_t *list)
{
list_t *node;
for (node = list; node; node = node->next)
printf("%s %d\n", node->text, node->count);
}
/*
simple file print of word list
*/
static void list_fprintf(FILE *fp, list_t *list)
{
list_t *node;
for (node = list; node; node = node->next)
fprintf(fp, "%s %d\n", node->text, node->count);
}
list_t *word_list = NULL; /* our word list */
int main(int argc, char **argv)
{
struct stat buff;
int size;
FILE *file = NULL;
unsigned char *data = NULL;
char *ptr, *text = NULL;
list_t *list = NULL;
/* check arguments for input */
if (argc < 2) {
printf("Usage: %s <INPUT> [OUTPUT]\n", argv[0]);
return -1;
}
/* get input file information */
if (stat(argv[1], &buff)) {
printf("Error: can't open file \"%s\"\n", argv[1]);
return -1;
}
size = buff.st_size;
if (size < 1) {
printf("Info: empty file\n");
return 0;
}
/* allocate memory to load the file */
if (!(data = malloc(size + 1))) {
printf("Error: not enough memory\n");
return -1;
}
/* now load the file to memory */
if (!(file = fopen(argv[1], "rb"))) {
printf("Error: can't open file \"%s\"\n", argv[1]);
return -1;
}
fread(data, size, 1, file);
data[size] = 0;
fclose(file);
/* reassign to standard output */
file = stdout;
/* now parse the data */
for (ptr = data; ptr - data < size; ) {
int c = *ptr;
if (isalnum(c)) {
if (!text) /* word starts */
text = ptr;
} else {
if (text) { /* word ends */
c = *ptr;
*ptr = 0;
/* add word to the list */
if (list = list_add(word_list, text))
word_list = list;
*ptr = c;
text = NULL; /* reset state */
}
}
ptr++;
}
/* free memory */
free(data);
/* optionally opens output file */
if (argc > 2) {
if (!(file = fopen(argv[2], "w"))) {
printf("Info: can't open \"%s\"\n", argv[2]);
file = stdout;
}
}
/* now output the list */
/*list_print(word_list); */
list_fprintf(file, word_list);
/* optionally close the file */
if (file && file != stdout)
fclose(file);
/* free the list */
list_free(word_list);
return 0; /* done */
}