Thread: help for beginner PLX!

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    41

    help for beginner PLX!

    HI! i'm just a beginner of c program, and i'm writing a program which requires user to type the words ,then it outputs the concert or close meaning on screen like a dictionary!


    A set of word and meaning was finished in a txt file, i designed to open an array then full each sentance in each element!

    i had done the input part, but for matching, i get a problem....i type the same word but it doesn't work....i dont know what i did wrong... please check the code below out and help me.

    and the txt file is enclosed,thx so MUCH!
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main ()
    {
        char* filename = "C:\\glossary_sorted.txt";
        FILE* glo;
        char* str[1000];
        char test[1000];
        int i ;
        int r=0;
    
      if (!(glo = fopen(filename,"r")))
          { printf("\nCannot open %s for output.\n",filename);
            return 1;
          }
    
      for(i = 0;i<1000;i++)
          {
              str[i] = (char*)calloc(100,sizeof(char));
          }
    
      while(!feof(glo))
          {
           fgets(str[r],1000,glo);
           r++;
          }
    
    printf("\ntest:");
    scanf("%1023s",test);
    printf("\nstr[2]:%s",str[2]);
    printf("\nyours:%s",test);
    
    
    if(strcmp(test,str[2])==0){
    printf("yes");}
    else{
    printf("\nWrong command. \n");}
    
    
    fclose(glo);
    
    
    return 0;}

  2. #2
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    i dont know if it is a good way to implement the program..but i thought for a long time..

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The words in your file are not consistent with their newlines - some have it, some don't.

    abstract class
    a class defined to make creating subclass easier
    array
    an ordered collection of values
    collection
    class that used for grouping and manipulating related objects
    compile time
    the time during which the source code is analyzed and converted into object code
    dictionary
    a collection of key/value pairs
    framework
    a collection of classes, functions and protocols that are related to support certain platform
    instance
    a concrete representation of a class
    message
    the method and its associated arguments that are sent to an object
    retain count
    a count of the number of times an object is referenced
    selector
    the name used to select the method to execute for an object
    But every word that fgets takes in, will have one.

    You can either remove the newline char from the next to the last position in the word you take in via fgets, or get your glossary set up with one word, one newline, and one end of string char, for every word.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    Quote Originally Posted by Adak View Post
    The words in your file are not consistent with their newlines - some have it, some don't.

    But every word that fgets takes in, will have one.

    You can either remove the newline char from the next to the last position in the word you take in via fgets, or get your glossary set up with one word, one newline, and one end of string char, for every word.
    but the glossary can not be changed...

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Yes, but you need to strip the trailing \n and/or \r characters when you read the lines in using fgets(). At the moment, str[2] is "array\n" but what you've read in from the user is "array".

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    Quote Originally Posted by JohnGraham View Post
    Yes, but you need to strip the trailing \n and/or \r characters when you read the lines in using fgets(). At the moment, str[2] is "array\n" but what you've read in from the user is "array".
    OH!thx!!that's why it doesn't work...but how to strip?

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1
    Code:
    while(!feof(glo))
    {
         fgets(str[r],1000,glo);
         r++;
    }
    Aside from the newline problem, you should not be using feof to control your loop. Rather, in this case you should test the result of the read operation (fgets):
    Code:
    while(fgets(str[r],1000,glo) != NULL)
    {
        r++;
    }

    #2
    Code:
    char test[1000];
    
    ...
    
    scanf("%1023s",test);
    I doubt you would ever come close to the limits here but those should match if you're going to do it that way.
    [edit]Actually the value in the scanf would need to be 1 less I think... I can't remember.[/edit]



    #3
    Code:
    for(i = 0;i<1000;i++)
    {
        str[i] = (char*)calloc(100,sizeof(char));
    }
    Avoid casting the return value of calloc/malloc calls in C. Also, sizeof(char) is guaranteed to be 1. You also do not check that the memory allocation succeeds, always assuming everything is OK. Finally, there is no de-allocation/freeing of this allocated memory. I'd personally would tend to just skip the dynamic allocation in this instance and go with a char[1000][100] array. 100KB is not that much.


    #4
    Code:
    char* filename = "C:\\glossary_sorted.txt";
    Bit of a nit, but that should be a const char* and not a char *.


    #5
    Quote Originally Posted by anthonyung
    OH!thx!!that's why it doesn't work...but how to strip?
    Find the last character of the array and set it to NULL, this will effectively shorten it by 1 character eliminating the pesky newline.
    Last edited by hk_mp5kpdw; 09-29-2010 at 08:21 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by anthonyung View Post
    OH!thx!!that's why it doesn't work...but how to strip?
    After you fgets() a line, search through and if you find a '\n' or '\r' character, set it to 0. Something like:

    Code:
    // Note: not tested
    for (int i = 0 ; str[i] != 0 ; i++) {
        if (str[i] == '\n') {
            str[i] = '\0';
            break;
        }
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    For example, the first "word" in your str[] array is "abstract class\n". So whether you're looking for abstract or class, you won't find it with strcmp().

    If you need to keep the glossary text in that format, you need to change things around a bit. One way would be to use strtok() and separate all the words that way. Another way would be to use strstr(), and look for your target word, within the row of words you have in your glossary file rows.

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    oh! thank you all for helping, i'm trying to edit my code,plx look forward to my result!

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    Quote Originally Posted by JohnGraham View Post
    After you fgets() a line, search through and if you find a '\n' or '\r' character, set it to 0. Something like:

    Code:
    // Note: not tested
    for (int i = 0 ; str[i] != 0 ; i++) {
        if (str[i] == '\n') {
            str[i] = '\0';
            break;
        }
    }
    i do it and the same result...

  12. #12
    Registered User
    Join Date
    Sep 2010
    Posts
    41
    fail...i dont find the last character of the array .
    i had used these 3 methods above ={..

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Post your complete new code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  14. #14
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Don't use scanf with strings. Use fgets(test, sizeof(test), stdin);.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't know if this is a great way to handle this - seems a bit clunky but may just need a function or logic tweak added to it.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define SIZE 120
    
    int main ()
    {
    
      FILE* glo;
      char str[SIZE][SIZE] = {{'\0'}};
      //    char* str[1000];
      char* filename = "glossary.txt";
      char test[SIZE];
      char *ptr;
      int i, len;
      int r=0;
      printf("\n\n");
    
      if (!(glo = fopen(filename,"r"))) { 
        printf("\nCannot open %s for output.\n",filename);
        return 1;
      }
      ptr = NULL;
      len = r = 0;
      while(fgets(test,SIZE,glo) != NULL) {
        ptr=strtok(test, " ");
        if(ptr) {
          strcpy(str[r], ptr);
          len=strlen(str[r]);
          if(str[r][len-1]=='\n')
            str[r][len-1]='\0';
          ++r;
        } 
        while(ptr) {
          ptr = strtok(NULL, " ");
          if(ptr) {
            strcpy(str[r], ptr);
            len=strlen(str[r]);
            if(str[r][len-1]=='\n')
              str[r][len-1]='\0';
            ++r;
          }
        }
      }
      i=0;
      while(i<r) {
        if(i % 20 == 0 && i) { 
          puts("\n\n        press enter to display the next set of words");
          (void) getchar();
        }
        printf("\n %d: %s ", i, str[i++]);
      }
    
      printf("\nEnter test word [class]: ");
      scanf("%1023s",test);
      printf("\nstr[2]:%s",str[1]);
      printf("\nyours:%s",test);
    
      if(strcmp(test,str[1])==0) {
        printf("\nyes");
      }
      else {
        printf("\nWrong command. \n");
      }
    
      fclose(glo);
      return 0;
    }
    Burst out words from your "glossary.txt" file:

    abstract
    class
    a
    class
    defined
    to
    make
    creating
    subclass
    easier
    array
    an
    ordered
    collection
    of
    values
    collection
    class
    that
    used
    for
    grouping
    and
    manipulating
    related
    objects
    compile
    time
    the
    time
    during
    which
    the
    source
    code
    is
    analyzed
    and
    converted
    into
    object
    code
    dictionary
    a
    collection
    of
    key/value
    pairs
    framework
    a
    collection
    of
    classes,
    functions
    and
    protocols
    that
    are
    related
    to
    support
    certain
    platform
    instance
    a
    concrete
    representation
    of
    a
    class
    message
    the
    method
    and
    its
    associated
    arguments
    that
    are
    sent
    to
    an
    object
    retain
    count
    a
    count
    of
    the
    number
    of
    times
    an
    object
    is
    referenced
    selector
    the
    name
    used
    to
    select
    the
    method
    to
    execute
    for
    an
    object

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 09-25-2002, 04:34 AM