Featured post
Input from file in C, data types -
thanks in advance taking time read through question...
/* binary search use gen_window.comp */ #include <stdio.h> int main() { char line[1000]; double sig, e; file *fp, *fopen(); fp = fopen("sig_data", "r"); if (fp==null){ printf("error opening file!"); } else { printf("processing file..."); } while( (fgets(line, 25, fp) != null) ){ fscanf(fp, "%lf\t%lf", &e, &sig); printf("\nthe energy is: %lf ev\nthe corresponding cross section is: %lf barns\n",e, sig); } }
numbers in file of format x.xxxxxx+x or x.xxxxxx-x accordingly. c have way specify input? bother there no 'e'. cannot find documentation tells inputing exponential, returning exponential...
i not need print exact format, other exponential format fine.
i realize there should specifiers , precision in %lf, or maybe should %e, have tried makes sense me, leaving blank.
thanks help.
jester , plinth's answers broadly correct, flawed. there indeed no way make stock number-parsing routines (*scanf
, atof
, strtod
) process number in format have. however, suggested workarounds bad. first off, personal opinion *scanf
, ato*
should never used, due horrifically bad handling of malformed input. more seriously, decimal-to-float conversion requires extended-precision arithmetic avoid loss of accuracy. strtod
(which routine should used problem) can relied on correctly, processing mantissa , exponent separately , combining them in obvious fashion not. need intermediate type bigger double
, need powers of 10 accurately computed way last bit, pow
doesn't reliably give you.
so recommendation rewrite each line on fly make acceptable strtod
.
- read each line pair of string buffers, splitting @
\t
. hand, can probably away usingfscanf
long use%s
formats. make sure each buffer has @ least 2 bytes of space available. - scan each buffer
+
or-
. usememmove
shift , subsequent bytes of string down one. pokee
buffer @ original position of sign. (if step 1 hand can combine step loop step.) - you have number in format
strtod
can process.
- Get link
- X
- Other Apps
Comments
Post a Comment