473,473 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sscanf'ing floats into structure members

(Using GCC) Simplified excerpt:

typedef struct {
double a;
double b;
double c;
} my_type;

int read(FILE* fp, my_type* data) {
char src_buffer[1024];
char q[16];
float utc_time;
...
sscanf(src_buffer, "%12s %f %f %f %f\n", q, &utc_time, &(data->a),
&(data->b), &(data->c));
}
Suppose that src_buffer looks like " 13:39:55.871 204234.000000
3746.741211 -12223.571289 20.279785\n".

For some reason, it is stuffing 0.0's into data->a, data->b, and
data->c. Why is that? I can't seem to figure out why this can possibly
be happening. Is there a problem with my implementation of what I am
trying to do? The alternative is to make other temporary floats and then
to assign them to data->a ... (which works), but this shouldn't be
necessary.

-Bernard Liang

Jun 21 '06 #1
3 3219
Bernard Liang schrieb:
(Using GCC) Simplified excerpt:
Do not give us "as if" code but a real, compiling minimal
example.
typedef struct {
double a;
double b;
double c;
} my_type;

int read(FILE* fp, my_type* data) {
Note: Make clear _what_ you are reading by giving the function
a telling name. As soon as you "read" a second kind of data,
you are stuck with read() and readOtherData().
char src_buffer[1024];
char q[16];
float utc_time;
...
sscanf(src_buffer, "%12s %f %f %f %f\n", q, &utc_time, &(data->a),
&(data->b), &(data->c));
Your forgot to check whether the return value of sscanf() is 5.

Your main mistake, though, is to use a float* format for a double*
argument. Use "%lf" instead.

You forgot to return something at the end of the function. }
Suppose that src_buffer looks like " 13:39:55.871 204234.000000
3746.741211 -12223.571289 20.279785\n".
Give us real input data. Avoid line breaks;
" 13:39:55.871 204234.000000"
" 3746.741211 -12223.571289"
" 20.279785\n"
is just as good.
For some reason, it is stuffing 0.0's into data->a, data->b, and
data->c. Why is that? I can't seem to figure out why this can possibly
be happening. Is there a problem with my implementation of what I am
trying to do? The alternative is to make other temporary floats and then
to assign them to data->a ... (which works), but this shouldn't be
necessary.


This usually is not necessary.

If the code you posted is close enough to your real code,
your problem is an FAQ,
http://www.c-faq.com/stdio/scanf2.html
If it is not, then this is an excellent example why you should
post real code.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 21 '06 #2


Bernard Liang wrote On 06/21/06 13:53,:
(Using GCC) Simplified excerpt:

typedef struct {
double a;
double b;
double c;
} my_type;

int read(FILE* fp, my_type* data) {
char src_buffer[1024];
char q[16];
float utc_time;
...
sscanf(src_buffer, "%12s %f %f %f %f\n", q, &utc_time, &(data->a),
&(data->b), &(data->c));
}
Suppose that src_buffer looks like " 13:39:55.871 204234.000000
3746.741211 -12223.571289 20.279785\n".

For some reason, it is stuffing 0.0's into data->a, data->b, and
data->c. Why is that?


Because "%f" generates a float value and stores it
via the matching float*. You want to store double values
through double* pointers, so you should use "%lf".

You mention you're using gcc, which happens to be one
of the compilers that can generate helpful warnings for
this sort of mismatch -- if you ask it politely:

gcc -W -Wall -ansi -pedantic ...

(You might want to specify a different level of Standard
conformance by changing -ansi to something else; check the
info screens.)

--
Er*********@sun.com

Jun 21 '06 #3
Bernard Liang <b_*****@berkeley.edu> writes:
(Using GCC) Simplified excerpt:

typedef struct {
double a;
double b;
double c;
} my_type;

int read(FILE* fp, my_type* data) {
char src_buffer[1024];
char q[16];
float utc_time;
...
sscanf(src_buffer, "%12s %f %f %f %f\n", q, &utc_time, &(data->a),
&(data->b), &(data->c));
}


For sscanf, "%f" expects a pointer to float. &utc_time is of the
correct type, but the following three arguments are all pointers to
double. Use "%lf" for a pointer to double (or "%Lf" for a pointer to
long double).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 21 '06 #4

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

Similar topics

13
by: - Steve - | last post by:
I've been given a school assignment and while everything else is easy there's one topic I'm completley lost on. I've been given an ASCII file that looks like this. During start-up, the program...
3
by: Evan Mathias | last post by:
Does anyone know a equivalent to sscanf from c++. I would appreciate a good answer to this one, since currently I have had to write a library of regular expressions to recognise integers, floats...
9
by: GrispernMix | last post by:
bool variant_t::Convert( fieldtype_t newType ) { if ( newType == fieldType ) { return true; } // // Converting to a null value is easy. //
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
7
by: gio | last post by:
suppose I have: .... char str1; char str2; int ret; fgets(str1, LEN, stdin); //str1 can contain just '\n' and '\0' ret=sscanf(str1, "%s", str2); ....
5
by: a | last post by:
After reading FAQ comp.lang.c section 12 and googling again, still there is no threads talking about reading a series of numbers. The input files, somehow structured, is exemplified below: ...
3
by: babayaga | last post by:
You would think this line would be 'easy' to parse with sscanf "*MESH_VERTEX 0 -36.9033 0.4202 31.5154" sscanf(line,"%s%i\t %f\t %f\t...
16
by: Chad | last post by:
Given the following #include <stdio.h> int main(void) { char line; long arg1, arg2; while(fgets(line, BUFSIZ, stdin) != NULL){
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.