CodeSteps

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

C Programming – Other commonly used functions when dealing with files

We have discussed some of the buffered and un-buffered functions those are dealing with file management in ‘C’. In this article, we are going to discuss couple of other ‘C’ functions used for file management.

Like fseek function we have lseek function which is helpful to navigate within the file. lseek function will be used with file descriptors; file descriptor will get when we open the file with open function call. The syntax of lseek function is like below:

off_t lseek(int fd, off_t offset, int whence);

Where fd is the open file descriptor; offset is the offset bytes within the file from the position mentioned through whence argument. We can pass SEEK_SET, SEEK_CUR and SEEK_END through whence argument. SEEK_SET moves position to offset bytes from beginning of the file, SEEK_CUR moves to offset bytes from current position and SEEK_END moves to offset bytes from end of the file.

Upon success lseek function will return the resulting offset location; otherwise it returns “-1” as an error.

Another commonly used buffered function is fprintf. fprintf prints to a file. The syntax of fprintf function is:

int fprintf(FILE *fp, const char *format, ...);

Where fp is the FILE pointer which we will get when we open the file using fopen function. Rest of the format is same like printf function. So I am not going to explain the same here.

printf function will print to the screen; where as fprintf function will print into the file.

What happened if we open the file using fopen function and attempt to read the contents using un-buffered function read?

Fortunately ‘C’ provides functions to deal with these scenarios; these functions are helpful to associate the FILE pointer to a file descriptor or vice-versa. ‘C’ provides following functions for this purpose:

int fileno(FILE *fp);

Where fp is the valid FILE pointer. Upon success this function returns the associated file descriptor.

And another function is:

FILE *fdopen(int fd, const char *mode);

Where fd is the valid file descriptor. fdopen function associates the file descriptor with FILE pointer and returns the FILE pointer upon success. mode is the string which controls on how to open the file (“r” for reading, “w” for writing, “r+” for both reading and writing “w+” also for both reading and writing, “a” for appending etc.,.).

Lets take a simple example:

// file.c
#include <stdio.h>
#include <stdlib.h>

void main()
{
   // Open the file using fopen()
   FILE *fp = fopen("file.c", "r");
   if ( fp == NULL )
   {
      printf("ERROR: Unable to open the file.\n");
      exit(0);
   }

   // Get file descriptor
   int count = 0;
   int fd = fileno(fp);

   if ( fd > 0 )
   {
      char buf[1025] = "";

      while ( ( count = read(fd, buf, 1024) ) > 0 )
      {
          // Add null at end; otherwise it will display junk data
          buf[count] = '';

          // Display it on screen
          printf("%s", buf);
      }

      // Move the file position to end;
      // and display the current position on the screen. 
      // End of the file position will be the size of the file.
      printf("\n--- Size of the file is %d bytes.---\n", lseek(fd, 0L, SEEK_END));
   }

   // Close the file
   fclose(fp);
}

I have added comments wherever required in the above code. So, I am not going to explain every statement in the code. The above code will read the file content from “file.c” and display on the screen along with the size of the file.

There are other functions also available for file management in ‘C’. I have covered commonly used functions in this series of articles.

I am closing this series of articles on file management with this article. If you have any comments or queries, please post here. I will try to respond as early as I can.

// Malin

C Programming – Other commonly used functions when dealing with files

Leave a Reply

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

Scroll to top