Convert scanf output to long double and discard the last newline
My goal is to read a one line file of a comma separated numbers into a floating point array. The numbers have up to 25 positions after the decimal. I'm having two issues with the following code.
1) atof() seems to be returning zeros every time. Why?
2) The last number includes the new line character. How do I get rid of it?
Note that I adapted the scanf command from here: The power of scanf() - Cprogramming.com, and don't completely understand it, either (so talk slowly when you answer).
Code:
#include <stdio.h>
#include <math.h>
//The following will be calculated in the real program.
#define DIM 1
#define N 8
int main()
{
char fn[] = "/raid/fpuData/bak/N=0008_Beta=09.00_tol=1e-08_t=0000.LC.csv";
FILE *LC = fopen(fn, "r");
int numElts=2*N;
double z[numElts];
char tmp[30];
int nextChar, i;
for (i=0; i<numElts; i++) {
// scanf("%[^,],", tmp);
fscanf(LC, "%[^,],", tmp);
printf("%d) %s\t\t", i+1, tmp);
z[i] = atof(tmp);
printf("%f\n", z[i]);
} // */
}
In the "real" program, N is calculated and known before reading in the file and the file will always have 2 times N numbers.
TIY