(1) Write file
#include <stdio.h>
#include <stdlib.h>#include <string.h>
int main(int argc, char *argv[])
{
FILE *fp;
double temperature[10];
double *temp;
int i = 0;
fp = fopen("temperature_data", "w");
for (i = 0; i < 10; i++)
{
temperature[i] = i;
}
temp = temperature;
for (i = 0; i < 10; i++)
{
fwrite(temp, sizeof(double), 1, fp);
temp = temp + 1;
}
// fwrite(temperature, sizeof(double), 10, fp);
fclose(fp);
(2) Read file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fp;
double temp[1];
int i = 0;
fp = fopen("temperature_data", "r");
printf("The data in temperature is: \n");
while(fread(temp, sizeof(double), 1, fp))
{
printf("temperature[%d] is: %f\n", i, temp[0]);
i++;
}
fclose(fp);
}