473,383 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Parsing problem

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

Nov 22 '06 #1
4 1361

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 '06 #2

dcor...@connx.com 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,
thanks for your time.

Nov 22 '06 #3

dc*****@connx.com 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.

Nov 22 '06 #4
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.

Nov 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
3
by: uestebanez | last post by:
Hi everybody! I have a little problem working with libxml SAX API. The problem is that I don't know how to stop parsing when I have processed the data I need. I don't want to parse all file, I...
5
by: gamehack | last post by:
Hi all, I was thinking about parsing equations but I can't think of any generic approach. Basically I have a struct called math_term which is something like: struct math_term { char sign; int...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
3
by: Anup Daware | last post by:
Hi Group, I am facing a strange problem here: I am trying to read xml response from a servlet using XmlTextWriter. I am able to read the read half of the xml and suddenly an exception:...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
1
by: Philip Semanchuk | last post by:
On Oct 12, 2008, at 5:25 AM, S.Selvam Siva wrote: Selvam, You can try to find them yourself using string parsing, but that's difficult. The closer you want to get to "perfect" at finding URLs...
2
by: Felipe De Bene | last post by:
I'm having problems parsing an HTML file with the following syntax : <TABLE cellspacing=0 cellpadding=0 ALIGN=CENTER BORDER=1 width='100%'> <TH BGCOLOR='#c0c0c0' Width='3%'>User ID</TH> <TH...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.