Week 5: IO Flashcards

1
Q

What arguments do the printf and scanf take?

A

Control string, other arguments

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

List the format specifiers that printf takes, and scanf takes…

A

printf( ) -> %d %c %s %f %e %g
scanf( ) -> %d %c %s %f %lf

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

What data type does the scanf( ) function read? How does it handle this data type?

A

Characters.
These are converted based on the format specifiers. For example, is the specifier is %d, scanf( ) reads a character from the input stream and converts it to a decimal integer.

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

When using scanf( ) in what structure does it receive data?

A

An input stream, in which the characters are then converted to the relevant type based on the corresponding format specifier.

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

What is an input stream? What 2 types are there?

A

A stream is a sequence of bytes. Streams can either be character based or binary based.

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

What does scanf( ) and printf( ) return?

A

scanf( ) -> The number of successful conversions.
printf( ) -> The number of characters printed to the console.

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

What is a stream?

A

An abstraction of a file

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

Why do we need to use streams for IO?

A

There is a speed disparity between IO and RAM. This causes the RAM to wait idly for IO processes to finish. By writing to streams, IO operations can store all data in the stream due to streams being buffered. When the stream is full of programmers chooses to, streams can be flushed, meaning they data is written and verified (read back) in an atomic action. Thus, the speed disparity between RAM and IO is reduced.

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

What does it mean if a stream is buffered?

A

It means they can store data until full or programmers chooses to flush them. In which case, the data is written to the a destination. This means that instead of writing data in a byte by byte way, which is cumbersome due to data validation needed on each byte written O(n), it can be done in an atomic action O(1).

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

What are the 3 most common file pointers? What is their function? Where do they write/read to/from?

A

stdin -> Standard input file -> Reads from the console.
stdout -> Standard output file -> Writes to the screen.
stderr -> Standard error file -> Writes to the screen.

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

What is the role of fprintf and fscanf ? What are the arguments of each? What is fprintf( stdin, control string, args ) equivalent to?

A

fprintf( ) -> Writes to a destination file -> ( file pointer, control string, args )
fscanf( ) -> Reads from a destination file -> ( file pointer, control string, args )
fprintf( stdin, control string, args ) == printf( ) because it is writing to the stdin file pointer.

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

In an abstract send, what is a file?

A

A character stream.

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

What does sscanf( )a and sprintf( ) do?

A

sscanf( ) -> Reads from it’s first argument into its other arguments.
sprintf( ) -> Writes other arguments into it’s first argument.

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

What functions are used to open and close a text file? Why do we need to open a file?

A

fopen and fclose.
We must open files as that’s the only way to manipulate them

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

What modes can we use with fopen?

A

r -> read
w -> write
a -> append
rb -> read binary
wb -> write binary
ab -> append binary
r+ -> Open a text file to read from and write to.
w+ -> Create a text file to read from and write to.
r+b -> Open a binary file to read from and write to.
w+b -> Create a binary file to read from and write to.
a+b -> Open a binary file for reading from and writing to.

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

What is the difference between fopen( “my_file.txt”, “r+” ) and fopen(“my_file.txt”, “w+”)?

A

r+ opens a text file to read from and write to.
w+ creates a text file to read from and write to.

17
Q

If you attempt to open a file for writing that doesn’t exist, what happens?

A

The file is created and opened.