#include <stdio.h> FILE *fp; |
#include <stdio.h> FILE *fp; fp = fopen("filename", "r"); better: if ( (fp = fopen("filename", "r") ) == NULL ) { cout << "Error: can't open file" << endl; exit(1); } |
fscanf(fp, "format string", &variable); |
#include <stdio.h> FILE *fp; fp = fopen("filename", "w"); // Overwrites (existing) file fp = fopen("filename", "a"); // Appends to (existing) file better: if ( (fp = fopen("filename", "w") ) == NULL ) { cout << "Error: can't create file" << endl; exit(1); } |
fclose(fp); |
fprintf(fp, "format string", &variable); |
(By induction, that means that you must read all the things preceding something first)
#include <stdio.h> fseek(fp, offset, whence); |
SEEK_SET |
The new position is equal to offset bytes from the beginning of the file |
SEEK_CUR |
The new position is equal to offset bytes from the current read position in the file |
SEEK_END |
The new position is equal to offset bytes from the end of the file |
fscanf(fp, "%d", &i); |
will read something unexpected if the next item is NOT an integer...