C Programming: Dealing with files (Part – 5)

As part of this series of articles, we are discussing about buffered and un-buffered functions dealing with file management. We already discussed about buffered functions in our previous articles; and started discussing on un-buffered functions. We have seen open and close functions in previous article. In this article we will discuss about few more un-buffered file management functions.

‘C’ provides read and write un-buffered functions to deal with files.

“read” reads data from files of given number of bytes. The syntax is:

size_t read(int fd, void *buf, size_t count);

Where fd is a valid file descriptor and buf is the buffer location where to store the read data. read function reads count number of bytes from the file. Upon success read function returns number of bytes read from the file. Upon failure it will return “-1”.

“write” function writes data into files. Below is the syntax:

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

fd must be the valid file descriptor and buf is the buffer where the data is available to write to the file. count is the number of bytes of data to write to the file. On success “write” function returns the number of bytes written into the file. It returns “-1” if the function failed to written into the file.

Lets write a simple program using these functions. This a copy program; which creates a copy of the file. Below is the code:

// sample.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

void main()
{
   // Open the file for read only.
   int fd = open("sample.c", O_RDONLY);
   if ( fd < 0 )
   {
      printf("ERROR: Unable to open the file.\n");
      exit(0);
   }

   // Open the file for writing purpose. 
   // If the file doesn't exist, create the file.
   int fdnew = open("copy-sample.c", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

   if ( fdnew < 0 )
   {
       printf("ERROR: Not able to create or open the file.\n");
       close(fd);
       exit(0);
   }

   size_t count = 0;
   char data[1025] = "";

   // Read from one file and write into another file.
   while ( ( count = read(fd, data, 1024) ) > 0 )
   {
      if ( write(fdnew, data, count) < 0 )
      { 
         printf("ERROR: Unable to write into the file.\n");
         break;
      }
   }

   if ( count > 0 )
   {
      if ( write(fdnew, data, count) < 0 )
      {
         printf("ERROR: Unable to write into the file.\n");
      }
   }

   // Close file(s).
   close(fdnew);
   close(fd);
}

Above sample program “sample.c” opens the file “sample.c”, read the content of the file and write to another file “copy-sample.c”. This program will create a file “copy-sample.c”, if it not exist; otherwise, it truncates the content of the file. It creates the file “copy-sample.c” with read, write permissions to the owner and only read permission to rest of the users.

If you type “ll copy-sample.c” at shell prompt, it will display below file permissions along with file details:

-rw-r--r--. 1 malin malin 1332 Jun  21 10:22 copy-sample.c

// Malin

Leave a Reply