Thread: fgets() question

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    22

    fgets() question

    I am trying to read user input from the terminal. If I enter "one two three four", how can I store these strings into argvs like argv[0] == one, argv[1] == two, argv[2] == three, argv[3]== four .. using fgets?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    fgets reads a whole line only. You will have to break the line up into words using something like strtok. Google for examples. If you need more complex parsing of the line, it would probably be best to write your own state machine.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    argv[] is used for command line input. If you wanted to do it that way, you will not need fgets
    Command Line Arguments in C - Cprogramming.com

    If you wanted to use fgets, you will have to create your own 2D array and use strtok + strncpy to enter your strings.
    C Strings - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    Code:
    int get_number() {
      char buffer[100]; //array that we are going to store the number in.
      int num;
      fgets(buffer, 100, stdin)
      sscanf(buffer, "%d", &num)
      return num;
    }
    This may help.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    That is putting your number into a char array, then converting it to an integer. (num).

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by gipper View Post
    Code:
    int get_number() {
      char buffer[100]; //array that we are going to store the number in.
      int num;
      fgets(buffer, 100, stdin)
      sscanf(buffer, "%d", &num)
      return num;
    }
    This may help.
    No - the OP indicated that they wanted to enter strings, not integers.

    And you left off a few semi-colons.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    Ahh, sorry. I completely read that wrong. I was thinking he needed help with something else.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by gipper View Post
    Ahh, sorry. I completely read that wrong. I was thinking he needed help with something else.
    That's ok - I do that all the time! Don't feel discouraged to continue offering help
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    Quote Originally Posted by Click_here View Post
    That's ok - I do that all the time! Don't feel discouraged to continue offering help
    Hahahaha, Thanks.

  10. #10
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help please , a fgets question
    By dogger in forum C Programming
    Replies: 4
    Last Post: 05-24-2011, 04:00 PM
  2. question about the fgets()
    By thungmail in forum C Programming
    Replies: 10
    Last Post: 11-05-2009, 03:19 PM
  3. fgets question
    By newbie30 in forum C Programming
    Replies: 5
    Last Post: 08-25-2009, 02:03 AM
  4. fgets question
    By mattyg in forum C Programming
    Replies: 2
    Last Post: 12-01-2008, 04:25 AM
  5. fgets question
    By Ash1981 in forum C Programming
    Replies: 12
    Last Post: 01-12-2006, 09:53 AM