Parsing problem

D

danu

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
 
D

dcorbit

danu said:
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?
 
D

danu

danu said:
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.​
 
D

danu

danu said:
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.​
 
S

santosh

danu said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top