1
votes

I am very new to C. I am trying to read the words from a file which contains lots of not alpha characters. My input file looks something like this %tOm12%64ToMmy%^$$6 and I want to read tom first and then put tom in my data structure and then read tommy and put that in my data structure all in lowercase. This is what I have tried until now. All my other code works as I have manually sent the parameters to the methods and there are no errors. This is what I have tried to read the words from the file. A word can be of 100 characters max. Can someone help me understand the logic and possibly this code.I am very lost.Thank You!

void read(FILE *fp)
{
  FILE *fp1 = fp;
  char word[100];
  int x;
  int counter = 0;

  while ((x = fgetc(fp1)) != EOF)
  {
     if (isalpha(x) == 0)
     {
        insert(&tree,word);
        counter = 0;
     }
     if (isalpha(x) != 0)
     {
        tolower(x);
        word[counter] = x;
        counter++;
     }
  }
  rewind(fp1);
  fclose(fp1);
}
2
first, tree variable is not defined here, so u should explain what it does. second, a char array should end with '\0' so make it "char word[101]" and end loop with "word[++counter] = '\0'CIsForCookies
You are missing the code to set the terminating null character of word. Add word[counter] = '\0'; before resetting counter.R Sahu
@CIsForCoocckies tree is a global variable and I did that but I am trying to make it so that it reads the first word which is tom and then insert it in the tree and then read the second word and insert into the tree, but the word does not clear after I insert tom. so how would I make it so it clears the array and then inserts a new word to the char array of the same 100 length.code4life
@RSahu I did that but the program compiles fine but the correct word is not sending. I am trying to make it so it reads tom and then sends to the tree. Then char array is cleared. It reads tommy sends to the tree and then char array is cleared. I added the word[counter]='\0'; but its not working.code4life
word[counter]=x; has no protection from x being too large.chux - Reinstate Monica

2 Answers

2
votes
char *getWord(FILE *fp){
    char word[100];
    int ch, i=0;

    while(EOF!=(ch=fgetc(fp)) && !isalpha(ch))
        ;//skip
    if(ch == EOF)
        return NULL;
    do{
        word[i++] = tolower(ch);
    }while(EOF!=(ch=fgetc(fp)) && isalpha(ch));

    word[i]='\0';
    return strdup(word);
}
void read(FILE *fp){
    char *word;
    while(word=getWord(fp)){
        insert(&tree, word);
    }
    //rewind(fp1);
    fclose(fp);
}
1
votes

This is a simplification of @BLUEPIXY 's answer. It also checks the array boundaries for word[]

char *getword(FILE *fp)
{
    char word[100];
    int ch; 
    size_t idx ;

    for (idx=0; idx < sizeof word -1; ) {
        ch = fgetc(fp);
        if (ch == EOF) break;
        if (!isalpha(ch)) {
           if (!idx) continue; // Nothing read yet; skip this character
           else break; // we are beyond the current word
           }
        word[idx++] = tolower(ch);
        }
    if (!idx) return NULL; // No characters were successfully read
    word[idx] = '\0';
    return strdup(word);
}