File Access

File Access
CS-2301, System Programming
for Non-Majors
(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and
from C: How to Program, 5th and 6th editions, by Deitel and Deitel)
CS-2301, B-Term 2009
File Access
1
Input and Output
• So far, all input and output has been to
standard input and from standard output
• I.e.,
• scanf() or getchar()
• printf()
• The computer terminal or other “official” media
• In most application and professional
situations, you will need to read/write to
files
CS-2301, B-Term 2009
File Access
2
File – Definition
• A persistent, named piece of data that is
stored in the computer system for later use
• Can be very large (gigabytes) or small (0 bytes)
• Can live a very long time
• Typically used to remember stuff that
outlives a particular running program
• Files serve all sorts of purposes
• Documents, databases, web pages, source code,
object code, data from applications, …
CS-2301, B-Term 2009
File Access
3
Directory or Folder — Definition
• A special kind of file that is used to
organize other files and directories
• Names of files and directories are defined in their
containing directories
• (Usually) hierarchically organized
• /user/lauer/CodingExamples/craps.c
• S:\cs2301\public_html\b09\index.htm
• Name of file is last component of pathname
• Extension (e.g., .txt, .c, .htm, .doc) is by
convention
CS-2301, B-Term 2009
File Access
4
Organization of Files
• Most files are organized as linear sequences
of bytes
• Binary, text, international text
• In this course, we are most concerned with
text files!
CS-2301, B-Term 2009
File Access
5
File I/O in C
• Before you can access a file you must open
it!
• After you are done, you must close it!
#include <stdio.h>
…
FILE *fp;
FILE *fopen(char *name, char *mode);
int fclose(FILE *fp);
CS-2301, B-Term 2009
File Access
6
File I/O in C
• Before you can access a file you must open
it!
• After you are done, you must close it!
#include <stdio.h>
…
FILE *fp;
FILE *fopen(char *name, char *mode);
int fclose(FILE *fp);
CS-2301, B-Term 2009
File Access
7
FILE *fopen(char *name, char *mode)
• name may be
• Fully qualified:– e.g., C:\lauer\cs2301\lecture.ppt
• Relative to working directory:– lecture.ppt
• mode may be
– read only
"w" – write only (create or discard previous contents)
"a" – append only (write at end; create if necessary)
"r+" – read and update (i.e., read-write)
"w+" – read-write (create or discard previous contents)
"a+" – read-append (write at end; create if necessary)
Add "b" to mode string for binary files
• "r"
•
•
•
•
•
•
CS-2301, B-Term 2009
File Access
8
File Input
• int fscanf(FILE *fp, char *format,
…)
• Same as scanf, but from an open file
• int getc(FILE *fp)
• Gets one character from FILE *fp
• Same as getchar() but from the file
• In fact, see p. 161!
#define getchar() getc(stdin)
FILE *stdin;
/* declared in <stdio.h> */
CS-2301, B-Term 2009
File Access
9
File Output
• int fprintf(FILE *fp, char *format,
…)
• Same as printf, but to an open file
• int putc(FILE *fp)
• Puts one character to FILE *fp
• Same as getchar() but to the file
• In fact, see p. 161!
#define putchar() putc((c), stdout)
FILE *stdout;
/* declared in <stdio.h> */
CS-2301, B-Term 2009
File Access
10
Standard input and output
• stdin – the “standard” source of input
• Terminal keyboard
• Read-only
• Piped from a file using "<" or "|"
• stdout – the “standard” output destination
• Terminal window
• Append only
• Piped to a file using ">" or "|"
• stderr – another “standard” output destination
• Terminal window
• Append only
• Normally used for error messages
CS-2301, B-Term 2009
File Access
11
Using File I/O
• Lab 5, part 2
• Complete on your own time
• Programming Assignment #5
• Programming Assignment #6
• More or less anything you do in WPI or
professional applications
CS-2301, B-Term 2009
File Access
12
Questions?
CS-2301, B-Term 2009
File Access
13