Presentation is loading. Please wait.

Presentation is loading. Please wait.

File AccessCS-2301, B-Term 20091 File Access CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.

Similar presentations


Presentation on theme: "File AccessCS-2301, B-Term 20091 File Access CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd."— Presentation transcript:

1 File AccessCS-2301, B-Term 20091 File Access CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

2 File AccessCS-2301, B-Term 20092 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

3 File AccessCS-2301, B-Term 20093 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, …

4 File AccessCS-2301, B-Term 20094 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

5 File AccessCS-2301, B-Term 20095 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!

6 File AccessCS-2301, B-Term 20096 File I/O in C Before you can access a file you must open it! After you are done, you must close it! #include … FILE *fp; FILE *fopen(char *name, char *mode); int fclose(FILE *fp); A data type defined in

7 File AccessCS-2301, B-Term 20097 File I/O in C Before you can access a file you must open it! After you are done, you must close it! #include … FILE *fp; FILE *fopen(char *name, char *mode); int fclose(FILE *fp); The path name of the file. The “mode” of access.

8 File AccessCS-2301, B-Term 20098 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 "r" – 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

9 File AccessCS-2301, B-Term 20099 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 */

10 File AccessCS-2301, B-Term 200910 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 */

11 File AccessCS-2301, B-Term 200911 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

12 File AccessCS-2301, B-Term 200912 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

13 File AccessCS-2301, B-Term 200913 Questions?


Download ppt "File AccessCS-2301, B-Term 20091 File Access CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd."

Similar presentations


Ads by Google