Week 5: IO Flashcards
What arguments do the printf and scanf take?
Control string, other arguments
List the format specifiers that printf takes, and scanf takes…
printf( ) -> %d %c %s %f %e %g
scanf( ) -> %d %c %s %f %lf
What data type does the scanf( ) function read? How does it handle this data type?
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.
When using scanf( ) in what structure does it receive data?
An input stream, in which the characters are then converted to the relevant type based on the corresponding format specifier.
What is an input stream? What 2 types are there?
A stream is a sequence of bytes. Streams can either be character based or binary based.
What does scanf( ) and printf( ) return?
scanf( ) -> The number of successful conversions.
printf( ) -> The number of characters printed to the console.
What is a stream?
An abstraction of a file
Why do we need to use streams for IO?
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.
What does it mean if a stream is buffered?
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).
What are the 3 most common file pointers? What is their function? Where do they write/read to/from?
stdin -> Standard input file -> Reads from the console.
stdout -> Standard output file -> Writes to the screen.
stderr -> Standard error file -> Writes to the screen.
What is the role of fprintf and fscanf ? What are the arguments of each? What is fprintf( stdin, control string, args ) equivalent to?
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.
In an abstract send, what is a file?
A character stream.
What does sscanf( )a and sprintf( ) do?
sscanf( ) -> Reads from it’s first argument into its other arguments.
sprintf( ) -> Writes other arguments into it’s first argument.
What functions are used to open and close a text file? Why do we need to open a file?
fopen and fclose.
We must open files as that’s the only way to manipulate them
What modes can we use with fopen?
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.