Files and Streams Flashcards

(47 cards)

1
Q

What does the scanf() function do?

A

It’s the reverse of printf() - used for formatted input from the console.

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

What does the return value of scanf() represent?

A

The number of data items successfully assigned a value.

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

What’s the correct way to read an integer using scanf()?

A

scanf(“%d”, &k) - The ampersand before the variable name is required to pass by reference.

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

What is a stream in C?

A

An abstraction of a file that provides a consistent interface to the programmer, regardless of the actual device.

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

What are two key characteristics of streams?

A

Streams are buffered, and may be text or binary.

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

What’s the difference between text and binary streams?

A

Text streams are sequences of characters with possible character translation (e.g., newline → CR+LF), while binary streams are sequences of bytes with no character translation.

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

What header file is required to use stream functions?

A

<stdio.h>
</stdio.h>

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

What is the purpose of a file pointer?

A

A file pointer (FILE *) is used to identify and manage a stream for file operations.

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

How do you open a file in C?

A

Using fopen(): FILE *fp = fopen(“filename”, “mode”);

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

Why should you always check if a file was opened successfully?

A

Because fopen() returns NULL if it fails to open the file.

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

What is the correct pattern to check if a file opened successfully?

A

if ((fp = fopen(“filename”, “r”)) == NULL) {
/* error handling */
}

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

How do you close a file in C?

A

Using fclose(fp);

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

What does the “r” mode do when opening a file?

A

Opens a text file for reading from

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

Q: What does the “w” mode do when opening a file?

A

Creates a text file for writing to (overwrites if file exists).

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

What does the “a” mode do when opening a file?

A

Opens a text file for appending to (writing to the end of it).

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

What’s the difference between “r+” and “w+”?

A

“r+” opens an existing file for both reading and writing, while “w+” creates a new file (or truncates existing) for both reading and writing.

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

How do you specify a binary file mode?

A

By adding “b” to the mode, e.g., “rb”, “wb”, “ab”.

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

What is errno in C?

A

A global variable defined in errno.h that is updated by system functions when an error occurs.

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

How can you get the text description of an error based on errno?

A

Using strerror(errno) from <string.h>

20
Q

What does the ferror() function do?

A

Determines whether the most recent operation on a file produced an error.

21
Q

When is the value of ferror() reset?

A

It’s reset by each file operation.

22
Q

What function writes a single character to a stream?

A

fputc(ch, fp)

23
Q

What function writes a string to a stream?

A

fputs(str, fp)

24
Q

Does fputs() output the null terminator?

A

No, the null terminator is not output.

25
What function allows formatted output to a stream?
fprintf(fp, "format", arguments...)
26
What function reads a single character from a stream?
fgetc(fp)
27
What function reads a string from a stream?
fgets(str, count, fp)
28
How does `fgets()` determine when to stop reading?
It reads up to a newline character, or a maximum of `count-1` characters.
29
What function allows formatted input from a stream?
fscanf(fp, "format", &variables...)
30
What does `fflush(fp)` do?
Forces the output buffer to be written to the file.
31
What does `remove("filename")` do?
Deletes the specified file
32
What does `rewind(fp)` do?
Resets the file position indicator back to the beginning of the file
33
What does `fseek(fp, offset, whence)` do?
Moves the file position indicator to `offset` bytes from `whence` (beginning, end, or current position)
34
What does `sprintf()` do?
Like `fprintf()`, but outputs to a string buffer instead of a file.
35
What does `sscanf()` do?
Like `fscanf()`, but reads input from a string buffer instead of a file.
36
What header file is needed for `sprintf()` and `sscanf()`?
37
What are the three standard streams available in C programs?
`stdin` (input from terminal), `stdout` (normal output to terminal), and `stderr` (error messages to terminal).
38
What is `scanf()` equivalent to?
`fscanf(stdin, ...)`
39
What is `printf()` equivalent to?
`fprintf(stdout, ...)`
40
Why separate `stdout` and `stderr`?
To allow normal output and error messages to be redirected separately
41
How do you redirect `stdout` to a file in Linux?
Using the `>` operator: `program > output_file`
42
How do you redirect `stderr` to a file in Linux?
Using the `2>` operator: `program 2> error_file`
43
How do you redirect both `stdout` and `stderr` to the same file?
Using: `program > out.file 2>&1`
44
What does it mean that streams are buffered?
Data is not necessarily written to the device when the write command is issued.
45
When are streams automatically flushed?
At the end of each line when output to a terminal, when the buffer is full, or when the program ends.
46
What is the typical buffer size for streams?
Implementation dependent, but often 8192 bytes.
47
How can you ensure output is immediately written?
By using `fflush(stdout)` to manually flush the buffer.