2

I need read just one line which contains integers numbers delimited with whitespace

Input: 1 41 2 15 6 7 87 4 2 1 9 0

My code can read each number using

int number;
while(scanf("%d", &number) == 1) { 
    printf("%d", number);
}

The while-loop ends when I send some character, how I can I do to while-loop ends when user press enter?

What my program does
Input line 1: 1 2 3 4 5 6 --Here I press enter
Input line 2: b -Send some bizarre character to finish

What I want
Input line 1: 1 2 3 4 5 6 --Here I press enter and finish

Daniela Morais
  • 2,125
  • 7
  • 28
  • 49
  • @user3121023 No, I have a undefined size of input. E.g, input can be "1 2 3 5" or "9 8 7 32 12 3 45 6 73 123 9". The function fgets() need some size. – Daniela Morais Sep 03 '16 at 20:14
  • 1
    Fundamentally, if you want line-based input, don't use `scanf()` or `fscanf()` because neither of them pays the slightest attention to line boundaries — they treat newlines just like any other white space character. If you want to process a line of data, read a line of data ([`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) or [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html)) and then parse the line with [`sscanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sscanf.html). – Jonathan Leffler Sep 03 '16 at 20:34
  • See also [How to use `sscanf()` in loops?](http://stackoverflow.com/questions/3975236/) – Jonathan Leffler Sep 03 '16 at 20:41

2 Answers2

4

Your integers are normally separated by whitespace, except when you read the last one. In that case it is followed by a newline character:

char next_char;
int number;

while (scanf("%d%c", &number, &next_char) == 2) {
    /* some stuff */
    if (next_char == '\n') break;
}
md5
  • 23,373
  • 3
  • 44
  • 93
  • This is good as long as the input doesn't allow for a space before the end of the line. – J Earls Sep 03 '16 at 20:05
  • Note that 'white space' normally includes newlines as one of the valid characters. The [`isspace()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/isspace.html) and [`isblank()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/isblank.html) functions illustrate this. _[…Hmmm; the POSIX pages aren't all that helpful; `man isblank isspace` on Mac OS X shows `isblank()` recognizing tab and blank, but isspace recognizing blank, tab, newline, vertical tab, form feed, carriage return…]_ – Jonathan Leffler Sep 03 '16 at 20:35
  • @JonathanLeffler: Yup, that's right, I just miss the english word for this character ` `... What is it? – md5 Sep 03 '16 at 20:37
  • Probably blank or space (or possibly tab); see the extended version of my comment. – Jonathan Leffler Sep 03 '16 at 20:39
2

With a sufficient buffer, fgets will read up to and include a newline.
The values may be parsed with sscanf. For an unknown number of values, use the %n specifier to iterate through the input. %n records the number of characters processed by the scan. Here the characters used in each scan is accumulated in offset.

#include <stdio.h>

#define SIZE 200

int main( void)
{
    char input[SIZE] = { '\0'};
    int used = 0;
    int offset = 0;
    int number = 0;

    if ( ( fgets ( input, SIZE, stdin))) {
        while ( ( sscanf ( &input[offset], "%d%n", &number, &used)) == 1) {
            offset += used;
            printf ( "%d\n", number);
        }
    }
    else {
        fprintf ( stderr, "problem getting input\n");
    }

    return 0;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16