Help discarding a space in char array
I have some embedded sql code that reads data from a sql database and inserts the data into a ingres isql database. That works fine. My problem lies in the code when I use atof to convert a character field (set up by outside company) in sql to a double for the ingres database. This value should have been an integer in SQL as well. So I store this char field from sql into a variable called rslt_rec_char.Hardness and if there is a space before the number (seems to be only problem) I need to remove it so the atof function will work. Can someone help me make changes to the code below to do this? Thanks. The int is_double just looks for decimal point and the function is posted below also.
Code:
879 if (is_double(rslt_rec_char.Hardness))
880 rslt_rec.Hardness = atof(rslt_rec_char.Hardness);
881 else if (!strcmp(rslt_rec_char.Hardness,""))
882 rslt_rec.Hardness = 0.0;
883 else
884 {
885 rslt_rec.Hardness = 0.0;
886 sprintf(jnl.mesg, "ERROR: CIN %s. Hardness test value invalid - \"%s\".\n"
887 "Hardness in test record %s not recorded!",
888 rslt_rec_char.CIN, rslt_rec_char.Hardness, rslt_rec_char.ResultsID);
889 log_it(logfile,jnl);
890 send_email(db_nm,"Met lab test upload error",jnl.mesg);
891 }
Code:
1421 int
1422 is_double(char s[])
1423 {
1424 int index, decimal_point_found = 0;
1425
1426 if (!strcmp(s,"")) return 0;
1427
1428 for (NULL; *s && isspace(*s); s++);
1429 if (s[0] == '-') s++;
1430
1431 for (index = 0; index < strlen(s); index++)
1432 {
1433 if (s[index] == '.') decimal_point_found++;
1434 else if (!isdigit(s[index])) return 0;
1435 }
1436
1437 return ((decimal_point_found <= 1) ? 1 : 0);
1438 }
Made following changes and now get core dump
Code:
char hardness[256];
char *first_nonblank;
double hardness2;
strcpy(hardness,"23.345");
first_nonblank = hardness;
while (*first_nonblank)
{
if (isspace(*first_nonblank))
first_nonblank++;
else break;
}
hardness2 = atof(first_nonblank);
printf("%s\n", hardness2);
strcpy(hardness," 23.345");
first_nonblank = hardness;
while (*first_nonblank)
{
if (isspace(*first_nonblank))
first_nonblank++;
else break;
}
hardness2 = atof(first_nonblank);
printf("%s\n", hardness2);
Just ran into one problem
I just realized I need to discard the first space if one exists but if more than one exists do some error handling. So I only want the value if no space exists or if one exists. Other than that break out of loop and do some error handling. Any one care to help me adjust the loop laserlight posted?
Thanks in advance.