- You open a data file with code such as:
fp = fopen("my.file", "r");
Remember to check to make sure that fp is not NULL
after this is executed. Also, do not hardcode in the file name --
you are passing it in on the command line.
- You can allocate space with malloc or calloc.
For example to allocate an array of 10 doubles:
double *f;
f = (double *)calloc(10, sizeof(double));
if (f == NULL) {
fprintf(stderr, "failed to allocate space for f\n");
exit(1);
}
You of course will be allocating an array of int rather
than double and will be getting the number of elements
from the command line.
- You read in values from a file using fscanf.
For example:
fscanf(fp, "%lf", &(f[i]));
reads the next value from the file fp into the
array element f[i].