GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Aug-2004, 20:46
Max Max is offline
New Member
 
Join Date: Aug 2004
Posts: 2
Max is on a distinguished road

Debugging problem....


Hi everybody.
I really got stuck into a number of problems with my code. Namely, I am trying to debug this portion of code but whatever I have tried I could not fix it. If you have some ideas please let me know, I'd appreciate a lot. I've already lost so many hours.
Thank you.

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

#define NUM_OF_LETTERS 26
#define MAX_WORD_LEN 20

typedef struct wordcount {
  char *word;
  int count;
  struct wordcount *next;
} WordCount;

WordCount *ltable[NUM_OF_LETTERS];
char **stopwords;

int CreateSWArray(char *filename, int *pnumsw);
int ProcessInputFile(char *filename, int fileno);
int convertToLower(char *);
int isStopWord(char *word, int numstopwords);
int insertWord(char *word);
int insertIntoList(char *pw, WordCount **phead);
void Cleanup(int numstopwords);
void freestopwordarray(int numstopwords);
void freeltable();

int main(int argc , char *argv[])
{
  int numstopwords; /* for storing the number of stop words */

  /* Check the number of command line arguments */
  if (argc < 3) {
     fprintf(stderr, "Usage: a4 <stop words file> <input file>\n");
     exit(1);
  }

  /* Set up an array to hold stop words */
  if (!CreateSWArray(argv[1], &numstopwords)) {
     exit(1); 
  }

  if (!ProcessInputFile(argv[2], numstopwords)) {
     exit(1);
  } 
 
  if (!GenerateOutFile(argv[2])) {
     Cleanup(numstopwords);
     exit(1);
  }
     
  exit(0);
}

/* Create an array to hold all the stop words */
int CreateSWArray(char *filename, int *pnumsw)
{
  FILE *fstopwords;
  char line[MAX_WORD_LEN+2], c;
  int i;

  /* Check for the existence of the stopwords file */
  if ((fstopwords = fopen(filename, "r")) == NULL) {
     fprintf(stderr, "File %s does not exist\n", filename);
     return(0);
  } 

  /* Get the number of stop words and store it in an int variable */
  /* pointed by pnumsw                                            */
  while ((c = fgetc(fstopwords)) != EOF) {
     if (c == '\n')
        (*pnumsw)++;
  }
  
  /* Allocate memory for stopwords array */
  if ((stopwords = (char **)malloc(sizeof(char *)**pnumsw)) == NULL) {
     fprintf(stderr, "not enough memory\n");
     fclose(fstopwords);
     return(0);
  }
  
  /* Set the file position indicator to the beginning of the file */
  rewind(fstopwords); 

  /* Read stop words from file to memory pointed by the stopwords array */ 
  i=0;
  while (fgets(line, sizeof(line), fstopwords)) {
     if ((stopwords[i] = (char *)malloc(strlen(line))) == NULL) {
        fprintf(stderr, "not enough memory\n");
        fclose(fstopwords);
        freestopwordarray(i);
        return(0);
     }
     line[strlen(line)-1]='\0'; /* remove the newline character read from fgets() */
     strcpy(stopwords[i], line);
     i++;
  }
  
  fclose(fstopwords);
  return(1);
}

/* Extract words from the input file and remove stop words */ 
int ProcessInputFile(char *filename, int numstopwords) 
{
  FILE *fin;
  int i, insertstatus;
  char c, word[MAX_WORD_LEN+1];

  if ((fin = fopen(filename, "r")) == NULL) {
     fprintf(stderr, "File %s does not exist\n", filename);
     return(0);
  }
  
  i=0;
  while ((c = fgetc(fin)) != EOF) {
     if (isalpha(c))
        word[i++]=c;
     else {
       word[i]='\0';
       if (i!=0) {
          convertToLower(word);
          if (!isStopWord(word, numstopwords)) {
             insertstatus = insertWord(word);
             if (!insertstatus)
                return(0);
          }
       }
       i=0;
     }
  }
  fclose(fin);

  return(1);
}

/* Convert the word into lower cases */
int convertToLower(char * word)
{
  int i;
  for (i=0; i<strlen(word); i++)
     word[i] = tolower(word[i]);
} 

/* Check if word is a stop word */
int isStopWord(char *word, int numstopwords)
{
  int i;

  for (i=0; i<=numstopwords; i++)
     if (strcmp(stopwords[i], word) = 0)
        return(1);
  return(0);
}

/* Insert a word into ltable */
int insertWord(char *word)
{
  if (insertIntoList(word, &ltable[word[0]-'a']))
     return(1);
  else
     return(0);
}

/* Insert a word into a linked list */
int insertIntoList(char *word, WordCount **phead)
{
  WordCount *nwc;

  if (*phead == NULL) {
     if ((nwc = (WordCount *)malloc(sizeof(WordCount))) == NULL) {
        fprintf(stderr, "not enough memory\n");
        return(0);
     }

     if ((nwc->word = (char *)malloc(strlen(word)+1)) == NULL) {
        fprintf(stderr, "not enough memory\n");
        return(0);
     }

     strcpy(nwc->word, word);
     nwc->count = 1;
     nwc->next = NULL; 
     *phead = nwc;
  }
  else if (strcmp(word, (*phead)->word)<0) {
     if ((nwc = (WordCount *)malloc(sizeof(WordCount))) == NULL) {
        fprintf(stderr, "not enough memory\n");
        return(0);
     }
     if ((nwc->word = (char *)malloc(strlen(word)+1)) == NULL) {
        fprintf(stderr, "not enough memory\n");
        return(0);
     }

     strcpy(nwc->word, word);
     nwc->next = *phead;
     *phead = nwc;
  }
  else if (strcmp(word, (*phead)->word)==0) {
     (*phead)->count++;
  }
  else 
     insertIntoList(word, &((*phead)->next));
  return(1);
}

/* Generate outputfile */
int GenerateOutFile(char *infilename)
{
  FILE *outfile;
  WordCount *p;
  int i;
  char fn[20];

  
  if ((outfile = fopen("output", "w")) == NULL) {
     fprintf(stderr, "cannot create the output file\n");
     return(0);
  }
  for (i=0; i<NUM_OF_LETTERS; i++) {
     p = ltable[i];
     while (p != NULL) {
        fprintf(outfile, "%s %s %f\n", p->word, infilename, p->count);
        p = p->next;
     }
  }
  fclose(outfile);            
  return(1);
}

void Cleanup(int numstopwords)
{
  freestopwordarray(numstopwords);
  freeltable();
}

/* free the memory space dynamically allocated for the stopwords */
/* array, including the space for storing each stop word         */
void freestopwordarray(int numstopwords)
{
  int i;

  for (i=0; i<numstopwords; i++) 
     free(stopwords[i]);
  free(stopwords);
}

/* free the memory space dynamically allocated for the nodes in */
/* ltable, including the space for storing each word            */
void freeltable() 
{
  WordCount *p, *q;
  int i;

  for (i=0; i<NUM_OF_LETTERS; i++) {
     p = ltable[i];
     while (p != NULL) {
        q = p->next;
        free(p->word);
        free(p);
        p = q;
     }     
  } 
}
Last edited by dsmith : 02-Aug-2004 at 08:21. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 01-Aug-2004, 21:59
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Max
Hi everybody.
I really got stuck into a number of problems with my code. Namely, I am trying to debug this portion of code but whatever I have tried I could not fix it. If you have some ideas please let me know, I'd appreciate a lot. I've already lost so many hours.
Thank you.

Welcome to GID, Max. We'll do what we can. Initially though, I'd suggest (since you asked)
1) use code tags as the sticky post mentions. Unformatted code is almost impossible to read...
2) Post the relevant sections of the code. Trying to understand 263 lines of mostly undocumented code is probably not on most people's list of things to do. We don't even have an idea what you are trying to accomplish in this code...
3) Stating you have a number of problems then asking us to figure out what those problems are, then come up with the solution, is also not a good plan.

Please give us an idea of what the code is supposed to do, what is wrong, where you think the problem is, and post the section of code you think is responsible with enough detail that we don't have to guess.

With that, we almost always can help you solve anything...
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 01-Aug-2004, 23:28
Max Max is offline
New Member
 
Join Date: Aug 2004
Posts: 2
Max is on a distinguished road
The program is run by following command:

program.c stopWords file1

where stopWords file contains all words that are likely to appear in some text file so many times, those words are: will, no, yes, an, on, big, wont, etc.

so the program compares words from file1 with those in the stopWords and generates a new file that contains all words that appear in file 1 but not in stopWords.
Words are sorted alphabetically in the newly created file in the following style:

word nameOfTheFileWordOriginatesFrom NumberOfOccurances

Now i've got some requirements, and they are:

If a file fails to open for reading or writing, the program must print corresponding error message (shown in the program) to standard error, and free all the memory that has been dynamically allocated so far, and exit the program with exit(1).
If memory allocation fails because there is not enough memory available, you must print the string "not enough memory" followed by a newline character to standard error, free all the memory that has been dynamically allocated (if any) and exit the program with exit(1).
All memory dynamically allocated must be freed before the program terminates.
You can assume that a stop word or a word in the input text file contains at most 20 alphabetical letters.

THANK YOU A LOT!
  #4  
Old 02-Aug-2004, 14:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
So: what works for you, and what doesn't work?

Have you actually compiled the program? What compiler?

It has an error that prevents successful compilation. Show
us your compiler output.


Dave
  #5  
Old 04-Aug-2004, 05:59
Garth Farley Garth Farley is offline
Awaiting Email Confirmation
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
I'm using GCC, I get
Code:
debug.c: In function `isStopWord': debug.c:149: invalid lvalue in assignment

Tracking:
CPP / C++ / C Code:
/* Check if word is a stop word */
int isStopWord(char *word, int numstopwords)
{
  int i;

  for (i=0; i<=numstopwords; i++)
    if (strcmp(stopwords[i], word) = 0)  //Pling Pling!
      return(1);
  return(0);
}

UR gonna kick your self. You're attempting to assign 0 into a function in your if statement. i.e. You should have == instead of =.

Your compliler should have choked on this & failed.

I've not tested the program, it simply compiles for me now.
GF
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C I/O problem kelly80 C Programming Language 4 27-Apr-2004 21:15
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 08:53
problem with php5 cgi installation fab13 Apache Web Server Forum 3 19-Nov-2003 10:11
unwanted scrollbar problem kelly001 Web Design Forum 3 24-Oct-2003 11:44
join problem zuzupus MySQL / PHP Forum 0 14-Aug-2003 06:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:35.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.