Week 12: File I/O in C Flashcards

1
Q

What is the data type used to access files in C and what library is required?

A

FILE *

pointer

stdio.h is required

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

To open a file what command is used?

A

fopen()

format: fopen(“filename”, “mode”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the following file modes?

r
w
a
r+
w+
a+
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What three files can be used with files?

A

stdout

stdin

stderr

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What file function can you use to abruptly leave the file at any time?

A

exit()

exit(status);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the four ways to read and write files?

A

Format file I/O

Get and put a character

Get and put a line

Block read and write

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Formatted file input and output is done through what functions?

A

fprintf()

fscanf()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the syntaxes of fprintf() and fscanf()

A

fprintf(FILE *fp, “string”);

fscanf(FILE *fp, “%d”, &address);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Get and put characters have what functions?

A

fgetc()

fputc()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the formats of fgetc() and fputc()

A

fgetc(FILE *fp);

fputc(int c, FILE *fp)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the functions used for getting and putting lines?

A

fgets()

fputs()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the format of fgets() and fputs()

A

char *fgets(char *s, int n, FILE *fp)

int fputs(char*s, FILE *fp);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What functions are used for binary reading and writing?

A

fwrite()

fread()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

fread() and fwrite() have what formats?

A

int fwrite (void *buf, int size, int count, FILE *fp);

int fread (void *buf, int size, int count, FILE *fp);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What function closes a file?

A

fclose(FILE *fp);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does flushing do?

A

Clear the buffer to force to the disk

17
Q

What function is used to flush a file?

A

fflush(FILE *fp)

18
Q

What function can be used to go to the beginning of the file?

A

void rewind(FILE *fp)

19
Q

What function can be used to indicate where the current position in the file is?

A

long ftell(FILE *fp)

20
Q

What function can be used to better control your position in a file?

A

fseek()

21
Q

What is the format of fseek()

A

fseek(FILE *fp, long offset, int origin)

22
Q

What do the following mean?

SEEK_SET

SEEK_CUR

SEEK_END

A

SEEK_SET
move the indicator offset bytes from the beginning

SEEK_CUR
move the indicator offset bytes from its current position

SEEK_END
move the indicator offset bytes from the end

23
Q

What does EOF mean and why can be be problematic

A

EOF = end of file
The problem is that the byte of data read may actually be
indistinguishable from EOF

24
Q

What is the end of file function for binary vs text files?

A
Binary feof()
Text: EOF
25
Q

What are the functions for removing and renaming a file?

A

remove()

rename()

26
Q

When will rename() return an error?

A

Oldname does not exist

Newname already exists

27
Q

What function creates a temporary name?

A

tmpnam()

28
Q

If fopen() fails what is returned?

A

NULL

29
Q

if fopen() works what is returned?

A

A pointer to the file