thanks in advance for taking the time to read through my question…
/* Binary search for use with 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 the file are of the format X.XXXXXX+X or X.XXXXXX-X accordingly. Does C have a way to specify this as input? Is it a bother that there is no ‘e’. I cannot find any documentation that tells about inputing exponential, only returning exponential…
I do not need it to print that exact format, any other exponential format would be fine.
I realize there should be some specifiers and precision in my %lf, or maybe it should be %e, but I have tried everything that makes sense to me, so I am basically leaving it blank.
Thanks for your help.
Jester and plinth’s answers are broadly correct, but flawed. There is indeed no way to make the stock number-parsing routines (
*scanf,atof,strtod) process a number in the format you have. However, the suggested workarounds are bad. First off, it is my personal opinion that*scanfandato*should never be used, due to their horrifically bad handling of malformed input. More seriously, decimal-to-float conversion requires extended-precision arithmetic to avoid loss of accuracy.strtod(which is the routine that should be used for this problem) can be relied on to do this correctly, but processing the mantissa and exponent separately and then combining them in the obvious fashion does not. You need an intermediate type bigger thandoubleand you need powers of 10 that are accurately computed all the way to the last bit, whichpowdoesn’t reliably give you.So my recommendation is to rewrite each line on the fly to make it acceptable to
strtod.\t. I would do this by hand, but you can probably get away with usingfscanfas long as you only use%sformats. Make sure that each buffer has at least two extra bytes of space available.+or-. Usememmoveto shift that and all subsequent bytes of the string down one. Poke aneinto the buffer at the original position of the sign. (If you do step 1 by hand you can combine this step with the loop for that step.)strtodcan process.