CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

C Programming – Dealing with files using un-buffered functions

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

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

read function

“read” reads data from files of a 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 a number of bytes read from the file. Upon failure, it will return “-1”.

write function

“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. the 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 write into the file.

Let’s write a simple program using these functions. This is 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”, reads the content of the file, and writes to another file “copy-sample.c”. This program will create a file “copy-sample.c”, if it does not exist; otherwise, it truncates the content of the file. It creates the file “copy-sample.c” with reading, writes permissions to the owner, and only read permission to the 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

C Programming – Dealing with files using un-buffered functions

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top