FILE
.stdio.h
, which all accept FILE*
as one of their parameters, except fopen()
.stdio.h
Functionsfopen()
NULL
.FILE* ptr1 = fopen("read.txt", "r");
FILE* ptr2 = fopen("write.txt", "w");
FILE* ptr3 = fopen("append.txt", "a");
fclose()
(ptr1); fclose
fgetc()
fopen
ed in reading ("r"
) mode.char ch = fgetc(ptr1);
// `cat` command: read all the characters from a file and print them to the screen, one-by-one
char ch;
while ((ch = fgetc(ptr)) != EOF)
("%c", ch); printf
fputc()
fopen
ed in write or append more.('A', ptr2);
fputc('!', ptr2); fputc
// `cp` command: copy contents of ptr1 to ptr2
char ch;
while ((ch = fgetc(ptr)) != EOF)
(ch, ptr2); fputc
fread()
<qty>
units of size <size>
from the file pointed-to and store them in memory in a buffer (usually an array) pointed-to by <buffer>
fgetc()
fopen
ed in reading ("r"
) mode.// Read 10 integers from ptr into arr
int arr[10];
(arr, sizeof(int), 10, ptr); fread
// Store 80 doubles in a array of doubles size 80
double* arr2 = malloc(sizeof(double) * 80);
(arr2, sizeof(double), 80, ptr); feead
// Work like fgetc()
char c;
(&c, sizeof(char), 1, ptr); fread
fwrite()
<qty>
units of size <size>
to the file pointed-to by reading them from a buffer (e.g., an array) pointed to by <buffer>
.fopen
ed in write or append more.// Write ten elements an array of ints size 10 into ptr
int arr[10];
(arr, sizeof(int), 10, ptr); fwrite
// Write 80 doubles from an array of 80 doubles into ptr
double* arr2 = malloc(sizeof(double) * 80);
(arr2, sizeof(double), 80, ptr); fwrite
// Work like fputc()
char c;
(&c, sizeof(char), 1, ptr); fwrite
fgets()
: Get string from filefputs()
: Write string into filefprintf()
: Print formatted string to filefseek()
: Set position in streamftell()
: Tell position in streamfeof()
: Tells whether read to end of a fileferror()
: Indicates whether an error has occurred in working with a file