Velocity Reviews > Parsing problem

# Parsing problem

danu
Guest
Posts: n/a

 11-22-2006
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

dcorbit@connx.com
Guest
Posts: n/a

 11-22-2006

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?

danu
Guest
Posts: n/a

 11-22-2006

(E-Mail Removed) 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[left][right] = sscanf(line, "%d", &array[left][right]);
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,

danu
Guest
Posts: n/a

 11-22-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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[left][right]);
right++;
c = getchar();
if(c == '\n') left++;
else ungetc(c, stdin);
}

what am I doing wrong here? thanks for any help.

santosh
Guest
Posts: n/a

 11-22-2006
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.