Like Tree1Likes
  • 1 Post By CommonTater

grabbing a multi-word query from stdin

This is a discussion on grabbing a multi-word query from stdin within the C Programming forums, part of the General Programming Boards category; I'm trying to allow the following Assume program is running and it's time for input: hello word The program then ...

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

    grabbing a multi-word query from stdin

    I'm trying to allow the following

    Assume program is running and it's time for input:

    hello word

    The program then needs to take this input and for each word perform a search, breaking up that line into "hello," and "world." I'm having trouble coming up with a decent way to do this. I've tried using a 2d array, but it's just getting me deep into segfaults. Another idea I had was to do something like:

    get line from stdin as a char*

    then pass that string to another function that takes the string and its current index:

    get character from string at index,
    while character is not a ' ' or '\n' add it to a char[buffer].
    inc index, get another character.
    return strdup(word);

    This requires me to make the integer a pointer however, since I'm only returning the word. That way the index can be updated in main so that when its time to get the next word, the main string knows where it left off. My implementation of this is horrid, however, and is producing segfaults as well.

    I'll post my code below, but does anyone have any other ideas?

    Code:
    char* get_query()
    {
      char query[QUERY_LENGTH*QUERY_LIMIT];
      int index = 0;
      char c;
      while( (c = getchar()) != '\n')
        query[index++] = c;
      query[index] = '\0';
      return strdup(query);
    }
    
    char* get_next_query(char* queries, int* index)
    {
      char c;
      int i = 0;
      char word[BUFFER];
      //printf("Index came in as : %d \n", index);
      //printf("Char at index in query is: %c  \n", queries[index]);
      while(queries[*index] != ' ' || queries[*index] != '\n')
      {
        //printf("Queries[index] = %d %c \n", *index, queries[*index++]);
        word[i++] = queries[index++]; //segfaults
      }
      word[i] = '\0';
      return strdup(word);
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look in your library documentation for the gets() and strtok() functions.
    Read up a little on arrays of pointers to strings.
    I'm sure you'll figure it out...

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > Look in your library documentation for the gets() and strtok() functions.
    Shurely shome mishtake here - gets() - nah, surely not, must mean fgets()
    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.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > Look in your library documentation for the gets() and strtok() functions.
    Shurely shome mishtake here - gets() - nah, surely not, must mean fgets()
    DUH.... can we write that off to impending senior citizen status?
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fflush(stdin) query!
    By Milan Singh in forum C Programming
    Replies: 2
    Last Post: 07-26-2010, 03:35 AM
  2. fflush(stdin) query
    By anirban in forum C Programming
    Replies: 12
    Last Post: 07-23-2009, 09:32 AM
  3. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 11:13 AM
  4. Grabbing stdin
    By Morgan in forum C Programming
    Replies: 5
    Last Post: 11-01-2002, 09:20 PM
  5. ascii 10 in the stdin after fflush(stdin)??
    By Kev2 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-03-2002, 03:53 PM

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