Week 12: File I/O in C Flashcards
What is the data type used to access files in C and what library is required?
FILE *
pointer
stdio.h is required
To open a file what command is used?
fopen()
format: fopen(“filename”, “mode”)
What are the following file modes?
r w a r+ w+ a+
r : open the file for reading (NULL if it doesn’t exist)
w : create for writing. destroy old if file exists
a : open for writing. create if not there. start at the end-of-file
r+ : open for update (r/w). create if not there. start at the beginning.
w+ : create for r/w. destroy old if there
a+ : open for r/w. create if not there. start at the end-of-file
What three files can be used with files?
stdout
stdin
stderr
What file function can you use to abruptly leave the file at any time?
exit()
exit(status);
What are the four ways to read and write files?
Format file I/O
Get and put a character
Get and put a line
Block read and write
Formatted file input and output is done through what functions?
fprintf()
fscanf()
What are the syntaxes of fprintf() and fscanf()
fprintf(FILE *fp, “string”);
fscanf(FILE *fp, “%d”, &address);
Get and put characters have what functions?
fgetc()
fputc()
What are the formats of fgetc() and fputc()
fgetc(FILE *fp);
fputc(int c, FILE *fp)
What are the functions used for getting and putting lines?
fgets()
fputs()
What is the format of fgets() and fputs()
char *fgets(char *s, int n, FILE *fp)
int fputs(char*s, FILE *fp);
What functions are used for binary reading and writing?
fwrite()
fread()
fread() and fwrite() have what formats?
int fwrite (void *buf, int size, int count, FILE *fp);
int fread (void *buf, int size, int count, FILE *fp);
What function closes a file?
fclose(FILE *fp);