File I/O
#include <stdio.h> void main( ) { FILE *fp; int outgoing; int incoming; printf("Enter a value to write to MyFile: "); scanf("%d", &outgoing); fp = fopen("MyFile", "w"); fprintf(fp, "%d\n", outgoing); fclose(fp); fp = fopen("MyFile", "r"); fscanf(fp, "%d", &incoming); fclose(fp); printf("The value read in from the file id %d\n", incoming); }
Sample Output:
UNIX prompt >>a.out
Enter a value to write to MyFile: <user enters 98>
The value read in from the file id 98
UNIX prompt >> more MyFile
98
Note:
FOPEN and FCLOSE work the same way as they do in MATLAB.