Thread: scanf() long input

  1. #1
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42

    scanf() long input

    Hi,

    I'm expecting a very long input like up to 1000 values each as big as 1to100 (1 55 100 34 44 etc.). This is how I do it:
    Code:
    char buffer[4 * 1000];
    char *ptr;
    char val;
    int i;
    
    scanf("%s", buffer);
    ptr = buffer;
    for (i = 0; i < 1000; i++)
      {
        sprintf(val, "%d ", ptr);
        // update ptr and process val.
      }
    Is there any nicer way of doing it. I'm just not used to deal with input from external world as I'm an embedded system programmer.
    Thanks for any advice.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    %s will read until the next white space character so it will only get the first number (assuming they are separated by spaces).

    There are other formats that will use different delimiters and such, look into the scanf documentation.

    EDIT: That is, if you want to do it that way. You can just use scanf in the loop instead with the integer format specifier.

  3. #3
    Registered User nenpa8lo's Avatar
    Join Date
    Jan 2008
    Posts
    42
    scanf() did the job, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar won't take any input after scanf
    By pshirishreddy in forum C Programming
    Replies: 2
    Last Post: 08-02-2009, 11:46 AM
  2. input row of integers via scanf into an array...
    By noodles355 in forum C Programming
    Replies: 1
    Last Post: 11-20-2006, 10:12 AM
  3. Is this the perfect input function?
    By Brain Cell in forum C Programming
    Replies: 30
    Last Post: 10-20-2004, 07:41 PM
  4. Restrict scanf() input?
    By difficult.name in forum C Programming
    Replies: 4
    Last Post: 09-20-2004, 12:27 PM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM