Help in Dictionary Program

This is a discussion on Help in Dictionary Program within the C Programming forums, part of the General Programming Boards category; This is the shorter version. W means it's a word file, and D in the filename, means it's a definition. ...

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is the shorter version. W means it's a word file, and D in the filename, means it's a definition.

    The example you gave, with tabs (and I believe it will work fine with a space also, although I haven't tried it yet), was the test data. It needs to go into a file named "classify.txt", and be in the same directory as the program (for now).

    I'm touching up the definition file part of this, and will post it up in a bit.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 30
    
    int main() {
      int i, len;
      char n, s1[MAX], def1[80]={""};
      char file1[9] = {""}, appen[5]={".txt"}, tmp[9]={""};
      FILE *fp;
            //printf("\n Enter a word: ");  //lines like this are for debugging only
            //scanf("%s", s); (void) getchar();
      fp = fopen("classify.txt", "rt");
      if(fp==NULL) {
        printf("ERROR opening file - terminating");
        return 1;
      }
      i=0;
      while(1) {
        i = fscanf(fp, "%s%*c%[^\n]s", s1, def1); 
        if(i < 2)
          break;
        printf("\n%s    %s", s1, def1);
                //getch();   //printf("\n%s", s);
        len=strlen(s1);
        file1[0]=s1[0];
        file1[1]='W';
        itoa(len, tmp, 10);
        strcat(file1+2, tmp);
        strcat(file1, appen);
        printf("\nFilename is: %s",file1);
                //getch();
    
        //reset all the string arrays to 0
        for(i=0;i<9;i++) {
          file1[i]='\0';
          tmp[i]='\0';
        }
        for(i=0;i<MAX;i++)
          s1[i] = '\0';
        for(i=0;i<80;i++)
          def1[i]='\0';
      }
      fclose(fp);
      printf("\n\n\t\t\t     press enter when ready");
      (void) getchar(); 
      return 0;
    }
    So aba gets a filename of aW3.txt, and Abe would go in there, as well.
    Aardvark would go in aW8.txt, etc.

    Test this part out a bit. I haven't done much of that.

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    thanks Sir Adak
    ill just study it first.
    thanks a lot.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't like working files like this, but you can't have several score of files open at the same time, either.

    First: this needs to be in it's OWN directory, with no ?Wn.txt or ?Dn.txt, where ? is any letter, and n is any number. If a previous run's files are there, delete them first, then run this.

    I slowed it with a couple of very short pauses, to let the hardware handle the file work, with better reliability (imo).

    This program should be run just ONCE (once you know it's accurate), on the data. Any other words that need to be added, should be done by either another function, or by hand, with cut and paste, on the correct word and define file.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX 30
    
    int main() {
      int i, len;
      char n, s1[MAX], def1[80]={""};
      char file1[9] = {""}, appen[5]={".txt"}, tmp[9]={""};
      FILE *fp, *fpW, *fpD;
    
    
            //printf("\n Enter a word: ");
            //scanf("%s", s); (void) getchar();
      fp = fopen("classify.txt", "rt");
      
      if(fp==NULL) {
        printf("ERROR opening classify.txt file - terminating");
        return 1;
      }
      i=0;
      while(1) {
        i = fscanf(fp, "%s%*c%[^\n]s", s1, def1); 
        if(i < 2)
          break;
        printf("\n%s    %s", s1, def1);
                //getch(); 
        len=strlen(s1);
        file1[0]=s1[0];
        file1[1]='W';
        itoa(len, tmp, 10);
        strcat(file1+2, tmp);
        strcat(file1, appen);
        printf("\nFilename is: %s",file1);
        fpW = fopen(file1, "at");
    
        if(fpW == NULL) {
          printf("Error opening %s - terminating", file1);
          return 1;
        }
        fprintf(fpW, "%s\n", s1);
        fclose(fpW);
        delay(50);             
        file1[1]='D';
        fpD = fopen(file1, "at");
        if(fpD == NULL) {
          printf("Error opening %s - terminating", file1);
          return 1;
        }
        
        fprintf(fpD, "%s\n", def1);
        fclose(fpD);
        delay(50);
              //getch();
    
        //reset all the string arrays to 0
        for(i=0;i<9;i++) {
          file1[i]='\0';
          tmp[i]='\0';
        }
        for(i=0;i<MAX;i++)
          s1[i] = '\0';
        for(i=0;i<80;i++)
          def1[i]='\0';
      }
      fclose(fp);
      
      printf("\n\n\t\t\t     press enter when ready");
      (void) getchar(); 
      return 0;
    }
    It's late, and I'm nodding off. I'll check in with you, after I get some zzz's.
    This is the program that will actually write the respective files, so check it out. You are my beta tester.

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Seems one hell of a way to make a mountain out of a mole-hill to me...

    I think the OP needs to make their own code, rather than you impressing everyone with your complicated scanf calls.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
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, 04: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, 10: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21