ECE209_file.pptx
ECE 209 Fall 2014 File I/O
Intro to File I/O
We don’t always want to read from stdin or
write to stdout.
What if we want to read from a file? Write to a file?
• One approach:
Use Linux to “redirect” stdin/stdout to a file.
Limited – only one input file or one output file,
prohibits interaction with user AND file.
• Better approach:
Create a new stream,
associate the stream with a named file,
read/write characters via the stream from/to the file.
ECE 209 Fall 2014 File I/O
abc\n123…
abc\n123…
Input from file
Output to file
ECE 209 Fall 2014 File I/O
New type: FILE*
If we’re going to create a new stream, then we need
a way to refer to it. And we need to save it in a variable.
So what type should this variable be?
#include
FILE *stream;
The stdio library provides a type called FILE*,
to represent a stream.
• It looks like a pointer (and it is a pointer),
but we never dereference it. We just pass it
to I/O functions.
• stdin and stdout are FILE* values.
ECE 209 Fall 2014 File I/O
Opening a file: fopen()
We “open” a file to create a new stream.
FILE* fopen(char *name, char *mode);
Opens a file with the given name, returns a stream.
Second arg is a string that says what we want to
do with the file.
“r” – open the file for reading (from beginning)
“w” – open the file for writing (from beginning)
“a” – open the file for appending (write to end)
If file cannot be opened, returns NULL.
ECE 209 Fall 2014 File I/O
Read from stream: fscanf()
The fscanf function is just like scanf,
except that we explicitly specify a stream
instead of implicitly using stdin.
int fscanf(FILE* stream, char* format, …);
Example: Open a file named foo.txt
and read one decimal integer.
FILE* in;
int val;
in = fopen(“foo.txt”,”r”);
fscanf(in, “%d”, &val);
Should check whether
NULL was returned.
ECE 209 Fall 2014 File I/O
New value: EOF
The return value of fscanf is just like scanf:
number of values converted and stored.
If fscanf encounters the end of the file before finding
a value to convert, it returns EOF.
Example: Read all integers from file and compute sum.
FILE* in;
int val, sum = 0;
in = fopen(“foo.txt”,”r”);
while (fscanf(in, “%d”, &val) != EOF) {
sum += val;
}
Should check whether
NULL was returned.
ECE 209 Fall 2014 File I/O
Write to stream: fprintf()
The fprintf function is just like printf,
except that we explicitly specify a stream
instead of implicitly using stdout.
int fprintf(FILE* stream, char* format, …);
Example: Open a file named foo.txt
and write one decimal integer and linefeed.
FILE* out;
int val = 100;
out = fopen(“foo.txt”,”w”);
fprintf(out, “%d\n”, val);
Should check whether
NULL was returned.
ECE 209 Fall 2014 File I/O
Closing a stream: fclose()
We “close” a stream (and associated file)
when we’re done with it.
int fclose(FILE*);
Returns EOF if error, zero otherwise.
(For output stream, flushes any buffered data to file before closing.)
After closing, no reads/writes permitted on stream.
Your program should do this, but it is done
automatically when the program ends.
• There is a limit to the number of files that can be open
at one time, so closing is needed for a long-running program.