Take a look at this little program I wrote - demonstrates
file io and should be sufficiently commented.
Code:
#include <stdio.h>
int main (void)
{
int a = 10, b = 20, c = 30; /*variables you want to save*/
int x, y, z; /*just using these to show you reloading in operation*/
FILE *in; /*I could have used 1 pointer twice, but I decided this may be clearer for you*/
FILE *out;
/*SAVING DATA*/
if ((out = fopen("savefile.txt", "w")) == NULL)
{
printf ("Error opening file for writing");
return 1;
}
fprintf (out, "%d %d %d", a, b, c); /*writing out separated by spaces*/
fclose (out);
/*END SAVING DATA*/
/*LOADING DATA*/
if ((in = fopen ("savefile.txt", "r")) == NULL)
{
printf ("Error opening file for reading");
return 1;
}
fscanf (in, "%d %d %d", &x, &y, &z); /*reading in separated by spaces*/
fclose (in);
/*END LOADING DATA*/
printf ("\nX = %d, Y = %d, Z = %d\n\n", x, y, z);
printf ("Press Enter to quit");
getchar ();
return 0;
}