1

I am trying to read a file. I want to read each line from the file and check if there are any spelling error in that line.

For that I have added condition that data from file will store in buffer until it gets a new line characher '\n'. And after getting this line I want to empty the buffer and re insert the values in that.

Code I am using for the same is as follows:

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

#define W_COUNT 23800
#define MAX_LEN 100

char *dict[W_COUNT];
char buffer[MAX_LEN];
int num_words;      //No of Words
char *statement[W_COUNT];
char buffer1[MAX_LEN];

void read_dictionary();
void file_read(char *);
void spell_check();
int word_search(char*);

int main(int argc, char*argv[]){
  int i;
  if(argc < 2){
    printf("Expected Filename.\n");
    exit(0);
  }
  read_dictionary();
  file_read(argv[1]);
//  spell_check();
}

void read_dictionary(){
  FILE *fd;
  int i = 0;
  fd = fopen("dictionary", "r");
  while ( fscanf(fd,"%s",buffer) != EOF)
    dict[i++] = strdup(buffer);
  num_words = i;
  fclose(fd);
}

void file_read(char *filename){
  FILE *fd;
  int i = 0;
  char c;
  fd = fopen(filename,"r");
  /*while ( fscanf(fd,"%s",buffer1) != EOF)
  {
    word[i++] = strdup(buffer1);
    printf("File : %s\n", buffer1);
  }*/
  while ( ( c = fgetc(fd)) != EOF )
  {
    buffer1[i++] = tolower(c);
    if ( c == '\n')
    {
      //printf("New Line\n");
      spell_check();
      buffer1[i] = 0;

    }
    //buffer1[i] = 0;
  }
  printf("Statement : %s\n", buffer1);
  fclose(fd);
}

void spell_check(){
  char *str;
  str = strtok(buffer1," .?,!-");
  while( str != NULL){
    if(!word_search(str))
    printf("%s Not found.\n",str);
    str = strtok(0," .?,!-");
  }
}

int word_search(char *word){

  int high, low, mid;
  high = num_words - 1;
  low = 0;
  int found = 0;

  while (found == 0){
    mid = (low + high) / 2;
    if(strcmp(word, dict[mid]) == 0)
    return 1;
    else if(strcmp(word,dict[mid]) < 0)
    high = mid - 1;
    else
    low = mid + 1;
    if ( low > high)
    return 0;
  }
}

Any suggestions will be appreciated. Thank you in advance.

6
  • you can assign a null character to the starting of the buffer. Commented Mar 26, 2016 at 3:54
  • I tried but not working. It only loop through the first line. Commented Mar 26, 2016 at 3:57
  • In where you assign the null character in coding? Commented Mar 26, 2016 at 3:58
  • spell_check(); buffer1[0] = NULL; I have tried here.. Commented Mar 26, 2016 at 3:59
  • For each line reading you have to assign the value of i to 0. But you didn't do that. Then you have to to assign the null character to the buffer. Commented Mar 26, 2016 at 4:02

1 Answer 1

2
while ( ( c = fgetc(fd)) != EOF )
  {
    buffer1[i++] = tolower(c);
    if ( c == '\n')
    {
      //printf("New Line\n");
      spell_check();
      i = 0;
      buffer1[i] = 0;

    }
    //buffer1[i] = 0;
  }

For each line reading you have to assign the 0 to the i. After that you have to assign the null to the 0th position in the buffer. You can try the above code for loop it will work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.