Thread: check if the word exists in file

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    45

    check if the word exists in file

    hello ..
    i have a question
    i have file of words ( dictionary) and i have another file(list) with list of words that could be or could not be in (dictionary)
    so i need to read from (List) and check if it exist in (Dictionary)
    and i want to check if the word exist or not .. help me out please , i couldn't figure it out
    Code:
    #define LENGTH
    int main(void)
    {
      FILE *Dict_f;
      FILE *Word_f;
      int w, index, s;
      char word[LENGTH];             
      
      // open the dictionary file
      Dict_f = fopen("Dictionary.txt", "r");
      // check if the file opened
      if (Dict_f == NULL) {
        printf("Couldn't Open File: Dictionary.txt!\n");
        exit(-1);
      }
      
      // open the test text file
      Word_f = fopen("list.txt", "r");
      // check if the file opened
      if (Word_f == NULL) {
        printf("Couldn't Open File: list.txt!\n");
        exit(-1);
      }
    
      //extracting the word
      do {
        w = fscanf(Word_f, "%s", word);
        s = strlen(word);
        CheckExist(word)            // function to check if the word exist in Dict_f or not
      } while (w != EOF);           /* repeat until EOF           */
      
      fclose(Word_f);
      fclose(Dict_f);
      
      return 0;
    }
    i know i should use sth like strcmp but what other condition with it ?

    if (strcmp(word, ...)==0)


    thank you !
    Last edited by Salem; 10-27-2015 at 12:02 AM. Reason: Massive code cleanup of tags

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to compare each word from the list.txt with every word from the Dictionary.txt.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Umm, also LENGTH is #define'd but it has no value... I expect you meant for there to be a number there

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yeah, that might have been me trying to clean up the mess - I think it was something like #define LENGTH 20
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to check if file exists ?
    By jabka in forum C Programming
    Replies: 14
    Last Post: 10-14-2007, 09:21 PM
  2. Check If File Exists On FTP
    By stickman in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2006, 05:06 PM
  3. How to check if a file exists
    By ElastoManiac in forum C++ Programming
    Replies: 10
    Last Post: 12-05-2005, 05:20 PM
  4. How to check if a file exists
    By ElWhapo in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2004, 05:16 PM
  5. check if the file exists
    By ipe in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 09:18 AM