Parsing problem

Discussion in 'C Programming' started by danu, Nov 22, 2006.

  1. danu

    danu Guest

    Hello all,
    basically here's what I'm trying to do: I want to put 5, 10, -20 , 5 ,
    20 in to array[0][MAX], and
    -1, 5, 10, -2 into array[1][MAX] . All the input are coming through
    stdin. How can I parse these values assuming that value sets are
    separated by a blank line and all the inputs end with "EOF" character.
    thanks for any help.

    int array[MAX][MAX]

    5 10 -20
    5 20

    -1 -5 -10 -2
     
    danu, Nov 22, 2006
    #1
    1. Advertising

  2. danu

    Guest

    danu wrote:
    > Hello all,
    > basically here's what I'm trying to do: I want to put 5, 10, -20 , 5 ,
    > 20 in to array[0][MAX], and
    > -1, 5, 10, -2 into array[1][MAX] . All the input are coming through
    > stdin. How can I parse these values assuming that value sets are
    > separated by a blank line and all the inputs end with "EOF" character.
    > thanks for any help.
    >
    > int array[MAX][MAX]
    >
    > 5 10 -20
    > 5 20
    >
    > -1 -5 -10 -2


    What have you tried?
     
    , Nov 22, 2006
    #2
    1. Advertising

  3. danu

    danu Guest

    wrote:
    > danu wrote:
    > > Hello all,
    > > basically here's what I'm trying to do: I want to put 5, 10, -20 , 5 ,
    > > 20 in to array[0][MAX], and
    > > -1, 5, 10, -2 into array[1][MAX] . All the input are coming through
    > > stdin. How can I parse these values assuming that value sets are
    > > separated by a blank line and all the inputs end with "EOF" character.
    > > thanks for any help.
    > >
    > > int array[MAX][MAX]
    > >
    > > 5 10 -20
    > > 5 20
    > >
    > > -1 -5 -10 -2

    >
    > What have you tried?


    what I'm thinking of is:

    char line[80];

    int left = 0, right = 0;

    while(fgets(line, 80, stdin) != NULL) {
    array
    = sscanf(line, "%d", &array
    );
    right++;

    }

    but I know this wouldn't work, because I don't know how to look for the
    "\n" character , in order to increase "left" value and go to the next
    array. The other thing is , I'm not sure about how to use sscanf
    properly because I know if I do this I'm only looking for just a one
    integer value.
    any help would be appreciated,
    thanks for your time.​
     
    danu, Nov 22, 2006
    #3
  4. danu

    danu Guest

    wrote:
    > danu wrote:
    > > Hello all,
    > > basically here's what I'm trying to do: I want to put 5, 10, -20 , 5 ,
    > > 20 in to array[0][MAX], and
    > > -1, 5, 10, -2 into array[1][MAX] . All the input are coming through
    > > stdin. How can I parse these values assuming that value sets are
    > > separated by a blank line and all the inputs end with "EOF" character.
    > > thanks for any help.
    > >
    > > int array[MAX][MAX]
    > >
    > > 5 10 -20
    > > 5 20
    > >
    > > -1 -5 -10 -2

    >
    > What have you tried?


    Another try:

    int c;
    while(getchar() != EOF) {
    scanf("%d", &array
    );
    right++;
    c = getchar();
    if(c == '\n') left++;
    else ungetc(c, stdin);
    }

    what am I doing wrong here? thanks for any help.​
     
    danu, Nov 22, 2006
    #4
  5. danu

    santosh Guest

    danu wrote:
    > Hello all,
    > basically here's what I'm trying to do: I want to put 5, 10, -20 , 5 ,
    > 20 in to array[0][MAX], and
    > -1, 5, 10, -2 into array[1][MAX] . All the input are coming through
    > stdin. How can I parse these values assuming that value sets are
    > separated by a blank line and all the inputs end with "EOF" character.
    > thanks for any help.


    Usually stdin is line buffered, which means that input is delivered to
    your program when a newline is encountered.

    For ease of use, try CBFalconer's non-standard but fully portable
    ggets() function to eat in each line. Check that a complete line has
    been delivered and that input has not prematurely ended. Then convert
    each string into an int with strtol() or sscanf() and store to your
    array.
     
    santosh, Nov 22, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    913
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    598
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    623
    Christopher Diggins
    Jul 9, 2007
  4. Christopher Diggins
    Replies:
    0
    Views:
    452
    Christopher Diggins
    Jul 9, 2007
  5. John Levine
    Replies:
    0
    Views:
    755
    John Levine
    Feb 2, 2012
Loading...

Share This Page