#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <math.h>
#include <float.h>
#include <stdlib.h>
static char *
string_tok(
char *str)
{
static char *next = NULL;
if(str == NULL) {
str = next;
}
else {
next = str;
}
if(str == NULL) {
return NULL;
}
while(next && next[0] != '\n' && next[0] != '\r') {
if(*next == ',') {
*next = '\0';
next++;
return str;
}
next++;
}
if(*next == '\r' || *next == '\n') {
*next = '\0';
}
next = NULL;
return str;
}
void main()
{
FILE *filep = NULL;
char line[100] = {0};
char *tmp_str = NULL;
char *str_tok = NULL;
char *fieldsp[10];
int nfield = 0;
filep = fopen( (char *)"raj.in", "r" );
if(NULL != filep){
while(fgets(line, 1024, filep)){
for ( tmp_str = line; ( str_tok = string_tok( tmp_str ) ) != NULL; tmp_str = NULL){
fieldsp[nfield++] = str_tok;
}
double rec_amt = atof(fieldsp[4]);
printf("%.21f\n", rec_amt);
}
}
else {
printf("error");
}
return;
}
Input file content
0001,000000000000000001-11227286431,9000000000000000001-11227286431000000044795,10/26/2010,0000024.18
0002,000000000000000001-16809572197,9000000000000000001-16809572197000000044793,10/26/2010,0000164.24
output:
24.179999999999999715783
164.240000000000009094947
My question here is in the file record amount shown is "0000024.18" but when I do atof it is returning 24.179999999999999715783. Does any one has an idea why this is happening? Do we have any alternative other then rounding?
Your response is highly appreciated.
Thanks,
--Raj