473,396 Members | 1,748 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,396 software developers and data experts.

Parsing Variable # of Data Fields after fgets

I'm processing a control file comprised of many types of lines, with
some containing variable data. I have a problem parsing the following
data:
18 12.2 7.145 6.214 Phase distances
First, I've read the entire line and have manually extracted the
record type (18). Now I must parse the (3) real values that follow,
only knowing (from other data previously read) that there are 3 values
there. The number of blanks separating the fields can vary, too.
I can use sscanf, I suppose, by coding a series of switch-based calls
that have 1-n %f format specifiers and 1-n array element references -
but that's cumbersome and limiting (if I encounter more data than I've
coded for). There must be a way to do this common sort of thing, but I
can't see it. 8<{{ Any thoughts? TIA
Dec 24 '05 #1
7 1714
Michael R. Copeland said:
I'm processing a control file comprised of many types of lines, with
some containing variable data. I have a problem parsing the following
data:
18 12.2 7.145 6.214 Phase distances
First, I've read the entire line and have manually extracted the
record type (18). Now I must parse the (3) real values that follow,
only knowing (from other data previously read) that there are 3 values
there. The number of blanks separating the fields can vary, too.
I can use sscanf, I suppose, by coding a series of switch-based calls
that have 1-n %f format specifiers and 1-n array element references -
but that's cumbersome and limiting (if I encounter more data than I've
coded for). There must be a way to do this common sort of thing, but I
can't see it. 8<{{ Any thoughts? TIA


Consider the wonderful flexibility that a loop offers you.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 24 '05 #2
> > I have a problem parsing the following data:
18 12.2 7.145 6.214 Phase distances
First, I've read the entire line and have manually extracted the
record type (18). Now I must parse the (3) real values that follow,
only knowing (from other data previously read) that there are 3 values
there. The number of blanks separating the fields can vary, too.
I can use sscanf, I suppose, by coding a series of switch-based calls
that have 1-n %f format specifiers and 1-n array element references -
but that's cumbersome and limiting (if I encounter more data than I've
coded for). There must be a way to do this common sort of thing, but I
can't see it. 8<{{ Any thoughts? TIA


Consider the wonderful flexibility that a loop offers you.

Sure...but _how_ do I do it? With sscanf? Some other way? If
sscanf, please explain how it's done.
Dec 25 '05 #3
>> > I have a problem parsing the following data:
> 18 12.2 7.145 6.214 Phase distances
> First, I've read the entire line and have manually extracted the
> record type (18). Now I must parse the (3) real values that follow,
> only knowing (from other data previously read) that there are 3 values
> there. The number of blanks separating the fields can vary, too.
> I can use sscanf, I suppose, by coding a series of switch-based calls
> that have 1-n %f format specifiers and 1-n array element references -
> but that's cumbersome and limiting (if I encounter more data than I've
> coded for). There must be a way to do this common sort of thing, but I
> can't see it. 8<{{ Any thoughts? TIA


Consider the wonderful flexibility that a loop offers you.

Sure...but _how_ do I do it? With sscanf? Some other way? If
sscanf, please explain how it's done.


Functions such as strtol(), strtod(), and strtoul() give you back
a pointer to the *END* of the data it used for the number. If it's
NULL, there was an error, if it's not, that's a good starting point
for looking for the next number.

Gordon L. Burditt
Dec 25 '05 #4
Michael R. Copeland wrote:
I have a problem parsing the following data:
18 12.2 7.145 6.214 Phase distances
First, I've read the entire line and have manually extracted the
record type (18). Now I must parse the (3) real values that follow,
only knowing (from other data previously read) that there are 3 values
there. The number of blanks separating the fields can vary, too.
I can use sscanf, I suppose, by coding a series of switch-based calls
that have 1-n %f format specifiers and 1-n array element references -
but that's cumbersome and limiting (if I encounter more data than I've
coded for). There must be a way to do this common sort of thing, but I
can't see it. 8<{{ Any thoughts? TIA


Consider the wonderful flexibility that a loop offers you.


Sure...but _how_ do I do it? With sscanf? Some other way? If
sscanf, please explain how it's done.

#include <stdio.h>

int
main( void )
{
int x;
double a, b, c;
char inbuf[100];
char string[] = "18 12.2 7.145 6.214 Phase Differences\n";

sscanf(string, "%d", &x);
switch( x ) {
case 18 :
sscanf( string,
"%*d %lf %lf %lf %[^\n]%*c",
&a,
&b,
&c,
inbuf
);
break;

default :
break;
}

printf("%lf %lf %lf %s\n", a, b, c, inbuf);
return 0;
}
Dec 25 '05 #5
Michael R. Copeland wrote:
I have a problem parsing the following data:
18 12.2 7.145 6.214 Phase distances
First, I've read the entire line and have manually extracted the
record type (18). Now I must parse the (3) real values that follow,
only knowing (from other data previously read) that there are 3 values
there. The number of blanks separating the fields can vary, too.
I can use sscanf, I suppose, by coding a series of switch-based calls
that have 1-n %f format specifiers and 1-n array element references -
but that's cumbersome and limiting (if I encounter more data than I've
coded for). There must be a way to do this common sort of thing, but I
can't see it. 8<{{ Any thoughts? TIA


Consider the wonderful flexibility that a loop offers you.


Sure...but _how_ do I do it? With sscanf? Some other way? If
sscanf, please explain how it's done.


I forgot to mention that sscanf() is great for this kind of application.
It is very powerful and you should grab your manual and read
thouroughly how it works. And, there are other ways to do it also. If
you want I'll post another example of a different way to do it.

Stan.
Dec 25 '05 #6
Michael R. Copeland wrote:
I have a problem parsing the following data: 18 12.2 7.145
6.214 Phase distances First, I've read the entire line and
have manually extracted the record type (18). Now I must
parse the (3) real values that follow, only knowing (from
other data previously read) that there are 3 values there.
The number of blanks separating the fields can vary, too. I
can use sscanf, I suppose, by coding a series of
switch-based calls that have 1-n %f format specifiers and
1-n array element references - but that's cumbersome and
limiting (if I encounter more data than I've coded for).
There must be a way to do this common sort of thing, but I
can't see it. 8<{{ Any thoughts? TIA


Consider the wonderful flexibility that a loop offers you.

Sure...but _how_ do I do it? With sscanf? Some other way? If
sscanf, please explain how it's done.


Please do not remove attribution lines for material you quote.

For example:

double realvals[3];
integer i, rectype;

if (1 != scanf("%d", &rectype) barf();
else {
switch (rectype) {
....
18: for (i = 0; i < 3; i++) {
if (1 != scanf{"%d", %realvals[i]) {
barf(); break;
}
dothingswith(realvals);
flushln(stdin); /* NOT fflush, you write this */
break;
....
default:
barf(); /* unknown rectype */
}
}

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Dec 26 '05 #7
Michael R. Copeland said:
Consider the wonderful flexibility that a loop offers you.

Sure...but _how_ do I do it? With sscanf? Some other way? If
sscanf, please explain how it's done.


find n, the number of fields on the line (which, apparently, you can do
already), and then do this:

for(i = 0; i < n; i++)
{
read a field from the line into array element[i], and don't lose track of
how far you've got along the line
}
Sure you could use sscanf. Or strtod. Or a home-rolled routine. Or whatever.
C is a very flexible language.

Personally, for this, I'd use a function that captures a whole line as a
string - I have a home-rolled routine for doing this but you could use
fgets if your inputs are sufficiently tame - and then do the conversions
using strtod, because it facilitates progressing neatly through the string.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 26 '05 #8

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

Similar topics

9
by: Lisa | last post by:
I have a select statement that gives me the following results (for example) "test documentation/software product version document.doc" I need to parse the data to only grab everything between the...
9
by: Mantorok Redgormor | last post by:
If I am parsing a config file that uses '#' for comments and the config file itself is 1640 bytes, and the format is VARIABLE=VALUE, is it recommended to use a) fgetc (parse a character at a...
7
by: millerm | last post by:
I'm obviously new to C, and have been trying different things to get this done, but I'm at the end of the line and need some suggestions. I am reading a string in from a user, in the form of a...
8
by: Eric Anderson | last post by:
I have some files that sit on a FTP server. These files contain data stored in a tab-separated format. I need to download these files and insert/update them in a MySQL database. My current basic...
29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
6
by: bfowlkes | last post by:
Hello, I am trying to parse two pre-formatted text files and write them to a different files formatted in a different way. The story about this is I was hired along with about 20 other people...
6
by: finer recliner | last post by:
i've been playing with the below code for a while now, and i cant get it to work correctly. the user should be able to input text into a textbox and upon hitting submit, it will append that text to...
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: Rick Owen | last post by:
Greetings, I have a form that, when submitted, calls a plsql procedure. The form has a number of fields (text, hidden, select, radio) but the particular field that is giving me problems is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.