Lecture05 InputAndOutput
Lecture05 InputAndOutput
Updated: 19
th August, 2018
Department of Computing
Curtin University
1/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
2/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Outline
Intro to I/O
File I/O in C
Errors
Reading/Writing
Binary les
Standard Streams
3/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Introduction to I/O
4/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
5/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Streams
available.
written to disk.
6/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Streams Visualisation
7/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Buering (1)
8/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Buering (2)
Input Buering
I When you want the next character, it's already waiting in the
buer.
Output Buering
9/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
10/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Seeking
11/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
I This command will display the man page for the strlen()
function:
12/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
DESCRIPTION
The strlen() function calculates the length of
the string s, not including the terminating '\0'
character.
RETURN VALUE
The strlen() function returns the number of
characters in s.
13/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
14/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
15/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
FILE Pointers
Note
I You never need to deal with the FILE type itself, only FILE*.
I You just pass the pointer around to various C functions.
16/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Reality
Concept
17/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Memory Disk
FILE*
Conceptually,
points here.
Actual
le
Stream Technically
contents
information points here.
18/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Memory Disk
FILE*
Conceptually,
points here. Actual
le
Stream Technically
contents
information points here.
18/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Memory Disk
FILE*
Conceptually, Actual
points here. le
Stream Technically
contents
information points here.
18/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Memory Disk
FILE*
Actual
Conceptually, le
Stream Technically
points here. contents
information points here.
18/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Memory Disk
FILE*
Actual
le
Stream Technically
Conceptually, contents
information points here.
points here.
18/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
I Opens a le.
I Closes a le.
19/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
#include <stdio.h>
...
FILE* f;
f = fopen("filename.txt", "r");
fclose(f);
20/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
File Modes
Basic modes
"r" read from a le (starting at the beginning)
"w" write to a le (create or overwrite the le)
"a" write to a le (starting at the end appending)
21/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Update Modes
I You can switch between reading and writing (but you can't do
both simultaneously).
ush.
Update modes
"r+" read & write (starting at the beginning)
"w+" read & write (create or overwrite the le)
"a+" read & write (starting at the end)
22/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
23/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Errors
I I/O errors are not necessarily your fault (though they might
be).
24/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
25/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
fclose(f);
}
26/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
27/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
if(ferror(f)) {
perror("Error reading from 'file.txt'\n");
}
fclose(f);
}
28/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
I Individual characters.
I Lines of text.
I Binary data.
I You may (or may not!) know how many data elements are
stored in the le.
29/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
I The read functions fscanf, fgets, fgetc will all tell you
when the le has ended.
I That's the trick: you won't know until after you try to read
past the end of le.
I The last read operation always fails (unless you know in
30/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
31/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
fprintf() example
32/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
fscanf() continued
I fscanf() returns either:
I the number of items successfully read, OR
Note on EOF
I EOF is a preprocessor constant, guaranteed to be some
negative integer.
33/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
fputc() example
34/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
fgetc() continued
35/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Example
36/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
#define INPUT_SIZE 21
...
FILE* f = fopen("input.txt", "r");
char str[INPUT_SIZE];
...
if(fgets(str, INPUT_SIZE, f) == NULL)
/* Error or end-of-file. */
else
/* Success -- you can safely use str. */
37/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Warning
38/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Flushing fflush()
I Flushing an output stream can help prevent data loss (if the
program, OS or computer crashes).
40/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Binary les
41/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
42/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
43/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
#define LENGTH 5
...
int data[LENGTH] = {10, 20, 30, 40, 50};
FILE* f = fopen("output.txt", "wb");
...
fwrite(data, sizeof(int), LENGTH, f);
#define MAXLEN 5
...
int data[MAXLEN], length;
FILE* f = fopen("input.txt", "rb");
...
length = fread(data, sizeof(int), MAXLEN, f);
if(length < MAXLEN) ... /* EOF, error or success? */
45/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Seeking fseek()
I Can position the FILE pointer anywhere in the le.
46/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
rewind()
I Resets the FILE pointer to the start of the le.
I This may be useful when you need to read a le multiple times.
ftell()
I Reports the current location within a le.
I Takes a FILE*.
I Returns the location as a long (indicating the number of bytes
from the start of the le).
47/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Standard Streams
48/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
The Terminal
49/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Redirection
I The UNIX shell (sh, csh, bash, etc.) can redirect the
standard streams most commonly stdin and stdout:
I Say you normally run program with parameters ...:
50/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
The Terminal
Input stream
stdin Program
51/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
The Terminal
Input stream
stdin Program
input.txt
Piping
I The UNIX shell can pipe the output of one program to the
input of another.
I On the command-line:
52/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Piping Visualisation
The Terminal
stdin stdout
program1 program2
stdout stdin
The pipe
53/54
Intro to I/O File I/O in C Errors Reading/Writing Binary les Standard Streams
Coming Up
54/54