Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –

Similar presentations


Presentation on theme: "CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –"— Presentation transcript:

1 CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() – unlink() Review Midterm 1 Lecture 101CS 311 - Operating Systems I

2 Difference between open() and fopen() open() and fopen() used to open files. Difference between them – open() is more UNIX (or POSIX) related and not easily portable to other non-POSIX systems. – fopen() is present in “stdio.h” and easily portable to other platform. – open() directly communicates with the Linux kernel while fopen() does not depend on the kernel. Lecture 102CS 311 - Operating Systems I

3 Reading from a file using read() Reads specified number of bytes from current file pointer position and updates the pointer accordingly. Prototype of read() ssize_t read(int fd, void *buf, size_t count); fd – file descriptor pointing to a file from which contents are read. buf – Generic (void *) buffer array into which the contents are read. Usually we use “char *buf” to read characters from file. count – number of bytes to read from file. Updates the file pointer position accordingly. Returns the number of bytes actually read when successful or -1 when failure. “size_t” and “ssize_t” are available in stdlib.h “size_t” same as “unsigned int” and “ssize_t” – “signed int” Lecture 103CS 311 - Operating Systems I

4 More on read() Suppose a file, say “example.txt” is 50 bytes in size. int fd_rd, fd_wr, returnVal; char buf[50]; fd_rd = open(“example.txt”, O_RDONLY); fd_wr = open(“example.txt”, O_WRONLY); returnVal = read(fd_rd, buf, 25); //returnVal has 25 and pointer position shifted to 26 th byte in file. returnVal = read(fd_rd, buf, 50); //returnval will have only 25 and pointer position shifted to end of file. returnVal = read(fd_wr, buf, 25); //returnVal has -1 close(fd_rd); close(fd_wr); Lecture 10CS 311 - Operating Systems I4

5 Writing to a file: write() Write contents to a file from current file pointer position and update file pointer accordingly. Prototype of write ssize_t write(int fd, void *buf, size_t count); fd – file descriptor pointing to a file to which contents are written. buf – Generic (void *) buffer array from which the contents are written. count – number of bytes to write. Updates the file pointer position accordingly. Returns the number of bytes actually written when successful or -1 when failure. If the O_APPEND flag was set for fd, the file position is set to the end of the file before each write. Lecture 10CS 311 - Operating Systems I5

6 Moving in a file: lseek() Allows you to change file pointer position. Prototype of lseek() off_t lseek (int fd, off_t offset, int mode); fd – pointing a file in which the pointer needs to be moved. offset – number of bytes to move the pointer (positive – move forward, negative – move backward) mode – various modes listed below (included in stdio.h) SEEK_SET - offset relative to the start of the file. SEEK_CUR – offset relative to the current file position. SEEK_END – offset relative to the end of the file. Returns updated file pointer position if success or -1 when failure. “off_t” same as “long int” Lecture 10CS 311 - Operating Systems I6

7 Deleting a file: unlink() Removes hard link to a file (delete from the directory) Prototype of unlink() int unlink (const char* fileName); Returns 0 on success and -1 on failure If there is only one hard link (or a fd) to a file then unlink deallocates resources for that file. If there more than one links (or fds) to a file then unlink removes file from the directory listing but does not deallocate resources. File is deallocated only when all fds are closed. Lecture 10CS 311 - Operating Systems I7


Download ppt "CS 311 – Lecture 10 Outline Review open() and close() Difference between fopen() and open() File management system calls – read() – write() – lseek() –"

Similar presentations


Ads by Google