1 / 20

File I/O

File I/O. CS241 Discussion Section Fall 2007 Week 9. Overview. How do you get data in/out of a program? Unix solution: Files. Many different kinds of I/O are viewed, to some degree, as accessing a file stream: stdin, stdout, stderr, /dev/audio,

Download Presentation

File I/O

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File I/O CS241 Discussion Section Fall 2007 Week 9

  2. Overview How do you get data in/out of a program? Unix solution: Files. Many different kinds of I/O are viewed, to some degree, as accessing a file stream: stdin, stdout, stderr, /dev/audio, pipes (cat file | grep text), network socket, ...

  3. Unix file system tree / /bin/ /home/ /home/someuser/ /home/someuser/somefile.txt /usr/ /usr/bin/ /usr/lib/

  4. ... T e x t c o n t e n t s o f Files A file is structured as a sequence of bytes, modeled after the notion of a tape: “Text contents of some file go here.” start of file file offset (similar to tape head)

  5. I/O libraries User process stdio: fopen, fread, fwrite, fclose, ... (buffered I/O) open, read, write, close, select, poll, ... (direct to kernel I/O) kernel system call handler file I/O terminal I/O pipe I/O network I/O audio I/O Kernel

  6. Buffered I/O: stdio.h We've previously used stdio.h lightly: printf(3) fprintf(3) etc. The stdio functions performing buffering: Read a large chunk from a file When the user requests a few bytes, just read them out of the buffer Þ lower overhead

  7. Unbuffered kernel I/O Internally, stdio calls kernel I/O functions: read(2) write(2) open(2) close(2) These directly call into the kernel, and only read/write as much as caller requested.

  8. File descriptors stdio uses a FILE * to denote an open file: FILE *stdin, *stdout, *stderr; The kernel uses int's as file descriptors: 0 mean standard input 1 means standard output 2 means standard error ...

  9. File descriptors A file actslike a tape. fd = 1 ... Array in kernel: file offset Text contents of somefile go here...

  10. open(2) #include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int open(const char *pathname,int flags,mode_t mode); Flags set access: read, write, append, etc.

  11. open(2) examples Open “input.txt” for reading: int ifd = open(“input.txt”, O_RDONLY); Open “output.txt” for writing: int ofd = open(“output.txt”, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);

  12. close(2) #include <unistd.h>int close(int fd); Any file can be closed—even standard input!

  13. read(2) #include <unistd.h>ssize_t read(int fd,void*buf,size_t count); Blocks until data is available. Returns number of bytes read.

  14. read(2) example Read 4 bytes, and store them into buf int fd = open(…); char buf[4]; read(fd, buf, 4);

  15. write(2) #include <unistd.h>ssize_t write(int fd,void *buf,size_t count); Blocks until data can be written(e.g., pipes block: cat file | grep text). Returns number of bytes written.

  16. write(2) example Write 4 bytes from buf into a file int fd = open(…); char buf[5] = “text”; write(fd, buf, 4);

  17. lseek(2) #include <sys/types.h>#include <unistd.h>off_t lseek(int fd,off_t offset,int whence); Moves the file offset. whence is one of SEEK_SET, SEEK_CUR, or SEEK_END. Returns the new offset.

  18. lseek(2) examples Seek to 4 bytes after beginning: lseek(fd, 4, SEEK_SET); Seek to 4 bytes before end: lseek(fd, -4, SEEK_END); Jump forward 16 bytes: lseek(fd, 16, SEEK_CUR);

  19. Problem 1: show file contents Write a program to print the contents of a file to the terminal (similar to cat, but for one file). $ ./show-file somefile.txtText contents of somefile go here. Use open(2), read(2), write(2), close(2).

  20. Problem 2: get byte at offset Write a program to print the byte at a given offset in a file, or by default, the file length. $ ./get-byte somefile.txt35$ ./get-byte somefile.txt 3t Use lseek(2) plus functions from before.

More Related