Why is interacting with files important?:
- files allow data to persist after the program has finished executing
- all programs prior to now can only store data for the length of time the program is executing
How to interact with files?:
- declare a file pointer
FILE *file_pointer;
- read more about
FILE
here: http://www.cplusplus.com/reference/cstdio/FILE/ - review the lecture on pointers: Pointers Lecture
- read more about
- attach a file to the file pointer
file_pointer = fopen("test.txt", "r");
- read more about
fopen
here: http://www.cprogramming.com/tutorial/cfileio.html - understand the different modes to open a file:
r
: open for readingw
: open for writing (file need not exist)a
: open for appending (file need not exist)r+
: open for reading and writing, start at beginningw+
: open for reading and writing (overwrite file)a+
: open for reading and writing (append if file exists)
- read more about
- read/write to the file using
fscanf
/fprintf
- read more about
fscanf
here: http://www.cplusplus.com/reference/cstdio/fscanf/ - read more about
fprintf
here: http://www.cplusplus.com/reference/cstdio/fprintf/
- read more about
- close the file and file pointer
fclose(file_pointer);
- read more about
fclose
here: http://www.cplusplus.com/reference/cstdio/fclose/
- read more about