lec13 - CS240: Programming in C Lecture 13: File I/O...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CS240: Programming in C Lecture 13: File I/O Monday, March 28, 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How should C programs interact with the outside? Communicate via “standard” input and “standard” output Typically bound to the display Use redirection to read or write to a file a.out < inputfile a.out > outputfile when we use printf(), the results are written to outputfile rather than displayed on the screen. a.out < inputfile > outputfile 2 Monday, March 28, 2011
Background image of page 2
More general approach . .. Redirection is really part of the operating system, not part of the C language But, C provides a set of library functions for performing I/O We’ve used one such library extensively: stdio.h provides operations to read (getchar) and write (putchar) characters, print formatted strings (printf), read formatted strings (scanf), etc. 3 Monday, March 28, 2011
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stdio.h Also provides more general operations on files. A file is an abstraction of a non-volatile memory region: its contents remain even after the program exits C exposes the file abstraction using the FILE type: FILE *fp // *fp is a pointer to a file Can only access the file using the interfaces provided by the language 4 Monday, March 28, 2011
Background image of page 4
File Systems 5 File system: specifies how the information is organized on the disk and can be accessed Directories Files In UNIX the following are files Peripheral devices (keyboard, screen, etc.) Pipes (inter process communication) Sockets (communication via computer networks) Files representation Text files (human readable format) Binaries (for example executables files) Monday, March 28, 2011
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
System Calls 6 System calls: services provided by the operating system. C Library provides support such that a user can invoke system calls through C functions. Example: I/O operations (I/O access is slower than memory access) Memory allocation Monday, March 28, 2011
Background image of page 6
File manipulation Three basic actions: “open” the file: make the file available for manipulation read and write its contents No guarantee that these operations actually propagate effects to the underlying file system “close” the file: enforce that all the effects to the file are “committed” 7 Monday, March 28, 2011
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
File Descriptors Any opened file has associated a non- negative integer called file descriptor. For each program the operating system
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

lec13 - CS240: Programming in C Lecture 13: File I/O...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online