Thread: Help in Dictionary Program

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    15

    Post Help in Dictionary Program

    Good day!

    I am only a beginner at c programming and I need some help to clarify the correct use of fseek function. Here is my program this is only for letter a:

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #define SIZE 10
    #include <io.h>
    
    struct entry{
    char  results[50];
    };
    
    main()
    {
    
    char letter[SIZE];
    FILE *fp, *fp2;
    int  num =0;
    char add, newword[50], word[10], buffer[50];
    long size, len;
    struct entry buffers;
    
     printf ("Enter a letter:\t");
     gets(letter);
     strlwr(letter);
    if (letter[0] == 'a')
    {
      if ((fp = fopen ("a.txt", "r")) != NULL)
      {             
      printf("Type a word:\n");
      gets(word);
      len = strlen(word);
      fseek(fp, (long) (len*sizeof(struct entry)), 0);
      fread(&buffers, sizeof(struct entry), 1, fp); 
      fprintf(stdout, "%s", buffers.results);
       fclose(fp); 
        }}
    getch();
    }
    
    My target is to create a dictionary program.
    This is my expected output:
    
    Enter a leter:     a
    Enter a word:    abaculus
    
    abaculus	small tile for mosaic
    
    But the result of my actual program is inaccurate.
    
    hoping for your response. Thankyou.
    Last edited by Salem; 03-05-2011 at 02:01 AM. Reason: Code tags around just the code.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If a.txt contains variable length lines of text (a word, followed by its definition), then fseek is not useful.

    fseek works best on fixed length records (lines of text are variable length).

    You can use fseek() on a text file, but you need to produce a separate index file first. This is done by reading the whole text file once, and recording where each line starts by calling ftell(). You can then use these values in future fseek() calls to find the start of a given line.

    Post some lines of a.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
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    I shortened a.txt into this just for example.

    Word Definition
    aba garment of camel or goat hair; camel or goat-hair fabric
    abacinate to blind by putting red-hot copper basin near the eyes
    abactor cattle thief
    abaculus small tile for mosaic
    abaft toward or at the stern of a ship; further aft
    abampere unit equivalent to 10 amperes
    abapical at the lowest point
    abarticular not connected with the joint
    abasement action of humbling or degrading
    abasia inability to walk due to lack of muscular coordination
    abask in genial warmth

    the word is separated to meaning by a tab.
    can you show me how to use fseek on a separate index file?
    thanks for replying
    Last edited by jerico; 03-06-2011 at 01:48 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    On the face of it, it doesn't seem like the kind of problem where fseek() is useful - at least for a beginner.

    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, sizeof buff, fp ) != NULL ) {
      // compare the first word of buff with your search word
    }
    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.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    Code:
    printf("Type a word:\n");
    gets(word);
    
    while (fgets( word, sizeof(word), fp ) != NULL) 
      // compare the first word of word with your search word
      
      c = strcmpi(word, "aba");
      strcpy(buffer, word);
      if(c == 0)
    {  
    printf("%s", buffer);
    }
    why is it none is printed?
    or i just have wrong codes?

    Sir how about EOL it can be useful?
    thanks.
    really need help
    Last edited by jerico; 03-06-2011 at 02:55 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't understand, you're getting a word with gets() (unsafe by the way), and then you're overwriting it immediately, with another word.

    Can you back up to the logic, and forget the code for now?

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    sorry for that.
    can you help me correct my codes?
    Or can you give me some informations that will help to debug this program?
    thanks.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    Bump bump

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A full sized dictionary is beyond your level of programming, atm. To do it properly with that much data, requires some very sophisticated programming.

    However, some good results are well within reach.

    Since the words will all be kept sorted, a binary (fast) search can be used to locate it, in the file (if we're clever). And since the definition will be kept to 80 char's or less, we can match up the index numbers, like so:

    word[0] = "aardvark"
    define[0] = "A furry ground dwelling, medium sized insect eater, with a long snout. Native to southern Africa".

    The zeroes above are the key. The definitions can be in another file altogether, (which allows the fast search), but the words index, will always match the definitions index. That association can never change. If and when the words get resorted, the definitions will be moved to match that of the word.

    Instead of opening up one file at a time, with all the info we want, we'll open up two.

    BUT, we can do much better. Because we have more words than we can ever search through as we want to, so -- we'll divide them up (divide and conquer is right), by the length of the word:

    A8Word.txt would have "Aardvark", for instance. A8Def.txt would have the definition for Aardvark. For a program, it's easy to find the right filename, see?

    Now, we can do an incredibly fast search and keep our files relatively reasonable in size. A utility function or program will put all the words into the right file, for us. No need to try and do it all with cut and paste, by hand.

    Your initial program organization wouldn't allow you to do what you wanted, as far as I can see.

    Does that sound OK, or too much?
    Last edited by Adak; 03-06-2011 at 10:12 PM.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    Now, we can do an incredibly fast search and keep our files relatively reasonable in size. A utility function or program will put all the words into the right file, for us. No need to try and do it all with cut and paste, by hand.
    i do understand what you want to do. but that paragraph...
    how i can put all words into the right file without doing cut and paste at the notepad?

    thanks for your post it helps alot!

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'll show you.

    If you post up maybe 25 words and their definitions (limited to 80 chars worth, hopefully), then we can talk more specifics.

    If this is an assignment that's due tomorrow, you know it won't be ready that fast, right?

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    no, i have 3 more days to do it. ty
    i'll wait your reply.
    thanks

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, we'll make it an abbreviated version.

    Do you have a file with good formatting, with all the words in sorted order, and all the (short hopefully) definitions, listed?

    Post a small (25 or so word list and their definitions), so I can get a clear idea of what you have for data.

    I have the classify program ready to go. Sandman catch you?

    I'll use the few example words and def's you posted, up above then.
    Last edited by Adak; 03-06-2011 at 11:51 PM.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    Word Definition
    aba garment of camel or goat hair; camel or goat-hair fabric
    abacinate to blind by putting red-hot copper basin near the eyes
    abactor cattle thief
    abaculus small tile for mosaic
    abaft toward or at the stern of a ship; further aft
    abampere unit equivalent to 10 amperes
    abapical at the lowest point
    abarticular not connected with the joint
    abasement action of humbling or degrading
    abasia inability to walk due to lack of muscular coordination
    abask in genial warmth

    i used space to separate words and meanings coz tab doesn't work.

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    I'm from Philippines.
    You can post anytime your reply, i'll just comment. the time zone here is GMT +8.

    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework Help, simple program, unsure newbie :(
    By Aslan14 in forum C Programming
    Replies: 13
    Last Post: 11-14-2010, 05:07 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread