Dealing with CR (\n) from fopen in search.

This is a discussion on Dealing with CR (\n) from fopen in search. within the C Programming forums, part of the General Programming Boards category; I'm trying to compensate for the carriage returns given to a program both from a file read in, or a ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    21

    Dealing with CR (\n) from fopen in search.

    I'm trying to compensate for the carriage returns given to a program both from a file read in, or a word entered by a user followed by a carriage return.

    The search function doesn't appear to deal with a \n very well:

    Calling search:
    Code:
      printf("Now, what would you like to search for?\n");
                fgets(buffer, sizeof(buffer), stdin);
                sscanf(buffer, "%s", &query_word);
                search(root, query_word);
    Search function:

    Code:
    void search(struct node* rootnode, char* word)
    {
       struct node* new=NULL;       /* a new node */
       struct node* here=NULL;      /* where we are looking */
       struct node* root=NULL;
       here=rootnode;               /* start at the root */
       while (1)                    /* loop until I tell you to break */
       {  if (strcmp(word\n, here->data) == 0)
          {  /* the word is right here */
             printf("The word %s is in the tree!\n",word);
          }
          else if (strcmp(word, here->data) < 0)  /* need to look left */
          {  if (here->left == NULL)
             {  /* we've reached the end, and can add the new node here */
                break;
             }
             else  /* move left and loop */
             {
                here=here->left;
                if(strcmp(word, here->data) == 0)
                {
                   printf("The word %s is in the tree!\n", word);
                }
             }
          }
          else if (strcmp(word, here->data) > 0)  /* need to look right */
          {  if (here->right == NULL)
             {  /* we've reached the end, and can add the new node here */
                break;
             }
             else  /* move right and loop */
             {
                here=here->right;
                if(strcmp(word, here->data) == 0)
                {
                   printf("The word %s is in the tree!\n", word);
                }
             }
          }
          else
          {
             printf("The word %s is not in the tree!\n", word);
          }
       }
       /*return; */
    }
    How can I compensate for the new-line characters when searching?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    The search function doesn't appear to deal with a \n very well
    please think about your reader: he has both to find what you mean and what's wrong with your code. Could you be more precise when explaining your problem?

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
      printf("Now, what would you like to search for?\n");
                fgets(buffer, sizeof(buffer), stdin);
                strtok(buffer, "\n");
                sscanf(buffer, "&#37;s", &query_word);
                search(root, query_word);
    Fix your indentation though, bud.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Of course the other option is scanf() is more capable of handling this on its own than you would think. But I would just stick to using strtok().

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by root4 View Post
    please think about your reader: he has both to find what you mean and what's wrong with your code. Could you be more precise when explaining your problem?
    I agree 100%. The good news is I read programming languages much faster than spoken languages and usually just scan through code to begin with. I spotted an area of concern (the line that I added) then looked up and found it was what he was asking about.

    Which also goes to explain how come sometimes I will answer stuff that the OP never even asked--I was reading their dysfunctional code, and not their question.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    You may need to work with something like the following as there is not chomp eqavalent in c:

    Code:
    while (('\n' != (ch = getc(word))) && (EOF != ch)) continue;
    return ch;

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    if (strcmp(word\n, here->data) == 0)
    ???
    "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
    21
    Quote Originally Posted by master5001 View Post
    Code:
      printf("Now, what would you like to search for?\n");
                fgets(buffer, sizeof(buffer), stdin);
                strtok(buffer, "\n");
                sscanf(buffer, "%s", &query_word);
                search(root, query_word);
    Fix your indentation though, bud.
    I apologize for my misdirection. I've always held true to the mantra of man-pages, and being told never to use strtok with constant strings. If it's the closest equivalent of chomp, then I'll use it, otherwise...

    To clarify, I have the file, which obviously has sentinel characters, inserted into a binary tree, with which I am searching for a word as given to me by a user (from stdin with a carriage return)

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    How is query_word declared? Looks like its of type char, revise it to be an array of char.
    Last edited by itCbitC; 11-18-2008 at 04:34 PM.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Quote Originally Posted by itCbitC View Post
    How is query_word declared? Looks like its of type char, revise it to be an array of char.
    I declare it as a character array:

    Code:
       char query_word[MAX_LENGTH];
       welcome();
       FILE *file_ptr;
       char word[MAX_LENGTH];

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by mlsrar View Post
    I declare it as a character array:

    Code:
       char query_word[MAX_LENGTH];
       welcome();
       FILE *file_ptr;
       char word[MAX_LENGTH];
    In that case your sscanf() should be revised as it's incorrect.
    Code:
    sscanf(buffer, "%s", &query_word);

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    It's just
    sscanf(buffer, "&#37;s", query_word);

    given
    char query_word[MAX_LENGTH];
    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.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    It's just
    sscanf(buffer, "%s", query_word);

    given
    char query_word[MAX_LENGTH];
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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