This blog is under construction

Tuesday, 22 May 2012

Low level input and output functions in C - creat, open, read, write, close and lseek

Below are the built-in functions available in C which can be used to perform low level input output operations.

open():
     Opens a file on calling it and returns file descriptor on success.  Otherwise, it returns 1.

Syntax:
     int open(const char  *path, int oflag, mode_t mode);

where path is the name of the file, oflag can be either O_RDONLY(open for read only), O_WRONLY(open for write only) and O_RDWR(open for read or write) and the third argument provides access permission information(S_IREAD, S_IWRITE, S_IEXEC etc.)


creat():
     It creates a new file on calling it and returns file descriptor on success.  Otherwise, 1 is returned.

Syntax:
     int creat(char *name, mode_t mode);

where name is the name of the file and the second argument is mode which provides access permission details(S_IREAD, S_IWRITE, S_IEXEC etc).


read():
     Data can be read from the opened file using read function.

Syntax:
     ssize_t read(int filedes, void *buf, size_t nbytes);

the above function reads nbytes of data from the file referred by file descriptor filedes and writes the read data to buffer buf.  It returns 0 if there's no other data is to read.  Otherwise, it returns the number of bytes of data read.


write():
     Data can be written to a opened file using write function.

Syntax:
     ssize_t write(int fd, const void *buf, size_t count);

 writes count bytes from the buffer buf to the file referred by file descriptor fd.


close():
     It closes a opened file. It returns 0 on success, 1 on failure.

Syntax:
     int close(int filedes);

where filedes is the file descriptor.  


lseek():
     It re-positions the file offset to perform read or write operation.

Syntax:
     off_t lseek(int fd, off_t offset, int origin);

it sets the current position in the file referred by file descriptor fd to offset, which is taken relative to the position pointed by origin.


creat, open, read, write, lseek and close example in C

  #include <stdio.h>
  #include <fcntl.h>
  #include <unistd.h>
  #include <sys/stat.h>
  int main() {
        int fd1, fd2, n;
        char str[100];

        /* open an input file in read mode */
        fd1 = open("input.txt", O_RDONLY, 0);
        /* creat an output file with read write permissions */
        fd2 = creat("output.txt", 0666);

        /* read the data from input file and write it to output file */
        while ((n = read(fd1, str, 10)) > 0) {
                write(fd2, str, n);
        }

        /* move the cursor to the 12 byte of the input file */
        lseek(fd1, 12, 0);

        /* write the given text in output file */
        write(fd2, "\nMoved cursor to 12th bytes\n", 28);

        /* writes the contents of input file from 12 byte to EOF */
        while ((n = read(fd1, str, 10)) > 0) {
                write(fd2, str, n);
        }

        /* close both input and output files */
        close(fd1);
        close(fd2);
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ cat input.txt 
  hello world
  hello world
  hello world
  hello world
  jp@jp-VirtualBox:~/$ ./a.out

  jp@jp-VirtualBox:~/$
  jp@jp-VirtualBox:~/$ cat output.txt 
  hello world
  hello world
  hello world
  hello world

  Moved cursor to 12th bytes
  hello world
  hello world
  hello world




1 comment:

  1. Dell Laptop Repair Center in Noida is no.1 service center which provides door to door services in or its nearby areas. We have expert, technicians who can repair your laptop at your home. . Call us: 9891868324

    ReplyDelete