I/O Concepts - staff.um.edu.mtstaff.um.edu.mt/mvel3/files/cprog/7_IOConcepts.pdf · C Primer Plus...
Transcript of I/O Concepts - staff.um.edu.mtstaff.um.edu.mt/mvel3/files/cprog/7_IOConcepts.pdf · C Primer Plus...
CPS1011 2/14
Content
● Standard I/O● Buffered I/O● I/O streams and redirection● Mind the buffer● Defensive programming – input validation● An application UI template
CPS1011 3/14
Standard I/O (i)
● Aim: program for the same abstract environment independently of the actual hardware/OS underneath
● Characteristics– Buffered– I/O streams
CPS1011 4/14
Standard I/O (ii)
● Buffering (intermediate storage)● Why?
– Preprocessing e.g. cleansed input
Helll←o px←c[ENTER]
“Hello PC\n”
CPS1011 5/14
Standard I/O (iii)
● Systemwise– Minimize ring switches– Maximize I/Ospecific h/w (DMA)
Mediates I/Ofor all applications
Ring switch
CPS1011 6/14
Standard I/O (iv)
● Buffer flush– Buffer is emptied and made available to the
consumer (e.g. program, disk, video)● Types
– Line buffering: flushes on '\n' (Keyboard)– Block buffering: flushed when full (network, disk)
Listing 8.1<<<
CPS1011 7/14
Standard I/O (v)
● I/O stream– Byte byte byte byte byte byte … EOF– EndOfFile (stream!) marker– #define EOF 1
– 1 outside index space of a character set– Bash: ^D on a newline
● Why?– Intuitive abstraction: get next byte and process– Abstraction from actual source: does not matter whether the input is a file or console– Can take advantage of command shell redirection
● > output redirection e.g. myprog > myfileondisk.txt● >> output append redirection e.g. myprog > myexistingfileondisk.txt● < input redirection e.g. myprog < myexistingfileondisk.txt● Combined e.g. myprog < myexistingfileondisk.txt > mynewfileondisk.txt
Listing 8.2<<<
CPS1011 8/14
Mind the buffer (i)
● Do not forget that linebuffered keyboard always contains the final '\n' per input line
N [ENTER]Y [ENTER]
“N\nY\n”
getchar() →'N'getchar() →'\n'getchar() →'Y'getchar() →'\n'
CPS1011 9/14
Mind the buffer (ii)
● Disposing of unwanted content inside the input stream after having consumed the required parts, or else is not needed e.g. malformed
while ( getchar() != '\n' );
Listings 8.4 8.5 8.6<<<
CPS1011 10/14
Defensive Programming (i)
● Or not trusting the end user!– >> Enter a character and 2 integers
– >> Enter a string no longer than 80 characters
– >> Enter a lowerbound followed by an upperbound
● Will the user comply to these instructions?● Do not assume anything! Unsafe● Rather
– Input validation– If valid: continue with normal execution– If invalid:
1.Show meaningful error message
2.Dispose of incorrect content from input stream
3.Resume
CPS1011 11/14
Defensive Programming (ii)
int get_int(void){
… while (scanf("%d", &input) != 1) { while ((ch = getchar()) != '\n')
… printf(" is not an integer.\nPlease enter an ");
printf("integer value, such as 25, 178, or 3: ");…
CPS1011 12/14
Defensive Programming (iii)
ch = get_first();
while ( (ch < 'a' || ch > 'c') && ch != 'q')
{
printf("Please respond with a, b, c, or q.\n");
ch = get_first();
}
char get_first(void){
…
ch = getchar();
while (getchar() != '\n');Listings 8.7 8.8<<<
CPS1011 13/14
Defensive Programming (iv)
● Fuzzing tools– Specialize in generating garbage input aiming to
test an applications robustness– Most computer intrusions using software
exploitation misuse lack of input validation
CPS1011 14/14
CPS1011 Reading List
Textbook:● C Primer Plus (6th edition). Stephen Prata. Addison
Wesley, 2013. ISBN 9780321928429
Supplementary reading:● Expert C Programming: Deep C Secrets. Peter van der
Linden. Prentice Hall, 1994. ISBN 9780131774292● The C Programming Language. BW Kernighan, DM
Ritchie. PrenticeHall, 1988. ISBN 0131103628