Basic file functions

Linux has many system calls to handle file, the table below shows some of common system calls. You can see their manual pages for more details.

man 2 functionName
Function Description Returns
open Open a file If success, return a file descriptor, otherwise, return -1
close Close a file Return 0 on success, otherwise, return -1
read read data from a file On success, return the number of bytes that been read, otherwise return -1
write write data to a file On success, the number of bytes written is returned, otherwise, return -1
lseek seek to a specified position in a file Upon successful completion, return the resulting offset location as measured in bytes from the beginning of the file, otherwise, return -1

We use an example to show how to use these system calls. copy.c is a simple simulation of the Linux cp command. The copy command can be used to make a copy of a file or a directory. copy.c is only the basic functionality i.e., copy the content from one file to another. It takes in two arguments namely the source file and the destination file.

./copy sourceFile destinationFile
/*copy.c */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#define BUFF_SIZE 1024
int main(int argc, char* argv[])
{
    int src_fd,dest_fd,readByte,writeByte;
    char * buff[BUFF_SIZE];
    /* Check the input*/
    if(argc != 3)
    {
        printf("\nUsage: ./copy source_file destination_file\n");
        exit(EXIT_FAILURE);
    }
    /* Open source file with read only flag*/
    src_fd = open(argv[1],O_RDONLY);
    if(src_fd == -1)
    {
        perror("Source file open");
        exit(EXIT_FAILURE);
    }
    /* Open destination file with write only flag, if it is not existed,
  creat it with permission 666*/
    dest_fd = open(argv[2],O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if(dest_fd == -1)
    {
        perror("Destination file open");
        exit(EXIT_FAILURE);
    }
    /* Start data transfer from src file to dest file untill it reaches EOF*/
    while((readByte = read(src_fd,buff,BUFF_SIZE)) > 0)
    {
        if(write(dest_fd,buff,readByte) != readByte){
            perror("Write");
            exit(EXIT_FAILURE);
        }
    }
    if(readByte == -1){
        perror("Read");
        exit(EXIT_FAILURE);
    }
    close(src_fd);
    close(dest_fd);
    return 0;
}

Analysis: First, we need to include necessary header files. Man page of every system call tells you what header files you need to include to be able to use this system call. Second we define a buffer with constant size. The buffer is used to transfer data from the source file to destination file. Then we open source and destination files, source with O_RDONLY to make it read only, destination with O_WRONLY | O_CREAT to make it writable and to create destination file with 0644 file system permission flags. We run read() to read data from the source file and run write() to write data to the destination file. If number of bytes read and number of bytes written differ this indicates error.