C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 18th, 2003, 09:57 AM
PatLukwago PatLukwago is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 1 PatLukwago User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Help, input from a file

Iam trying to count the frequency of words read from a file, but when l try to run the program it just hungs up does nothing, lam relatively new in this field. My code is as shown bellow, what lam l not doing right?


#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#define MAXWORD 1000


struct tnode {
char *word;
int count;
struct tnode *left;
struct tnode *right;
};

struct tnode *addtree(struct tnode *, char *);
void treeprint(struct tnode *);
struct tnode *talloc(void);
int getword(char *, int);
char *strdupl(char *);


/* word frequency count */

int main(void)
{
struct tnode *root;
char word[MAXWORD];
root = NULL;

while (getword(word, MAXWORD) != EOF)
if (isprint(word[0]))
root = addtree(root, word);
treeprint(root);
exit (0);
}

/* addtree: add a node with w, at or below p */

struct tnode *addtree(struct tnode *p, char *w)
{
int cond;

if (p == NULL) { /* a new word has arrived */
p = talloc(); /* make a new node */
p->word = strdupl(w);
p->count = 1;
p->left = p->right = NULL;
}

else if ((cond = strcmp(w, p->word)) == 0)
(p->count)++; /* repeated word */
else if (cond < 0) /* less than into left subtree */
p->left = addtree(p->left,w);
else /* greater than into right subtree */
p->right = addtree(p->right,w);
return p;
}

/* treeprint: in-order print of tree p */

void treeprint(struct tnode *p)
{
if (p !=NULL) {
treeprint(p->left);
printf("%4d %s\n", p->count, p->word);
treeprint(p->right);
}
}


/* talloc: make a tnode */

struct tnode *talloc(void)
{
return (struct tnode *) malloc(sizeof(struct tnode));
}


/* getword: get next word or character from input */

int getword(char *word, int lim)
{

FILE *pFile;
int c;
char *w = word;

pFile = fopen ("/mnt/samba/k/patrick/testp.txt","r");

while (isspace(c = fgetc(pFile)))
;
if (c != EOF)
*w++ = tolower(c);
if (!isprint(c)) {
*w = '\0';
return c;
}
for ( ; --lim > 0; w++)
if(!isalnum(*w = fgetc(pFile)) && !ispunct(*w)) {
ungetc(*w,pFile);
break;
}

*w = '\0';
fclose(pFile);
return word[0];
}

/* strdupl: make duplicate of s. (strdup builtin) */

char *strdupl(char *s)
{
char *p;
p = (char *) malloc(strlen(s)+1);
if (p != NULL)
strcpy(p, s);
return p;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Help, input from a file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap