pointers - Read data into a float array in C -
I am creating a program where I need to load some temporary point data of the file from the file 103.45 < 123.45
456.67
I was wondering how to store these data directly in the array of floating point numbers using Fried () Go. I think it is not difficult to use pointers, but I am not so good with them. Anyone can tell me that
to read the data from the file sscanf ()
.
/ * Verification and error checking * / fgets (buf, sizeof buf, fp) is required; Sscanf (buf, "% f", and tmp);
To manage the number of float, you have two options.
- Use an array of fixed size
-
molok ()
,for a better array
and friends
/ * 1. Use an array of fixed size * / float array [1000];
/ * 2. Use `malloc ()`, `realloc ()` and friends for a growable array * / float * array; Array = maulock (1000 * size * array); / * ... Start to read the floats ... loop ... * (/ if array_needs_to_grow) {float * tmp = realloc (array, new_size); If (tmp == null) / * oops, error: no memory * /; Array = tmp; } / * End read loop * / / * ... use array ... * / free (array);
Comments
Post a Comment