CodeSteps

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

C Programming – Dealing with files – How to use open function to open a file?

We have discussed that ‘C’ provides buffered and un-buffered functions for file management. We have already discussed some of the buffered functions in our previous articles; “C Programming – Dealing with files – How to read from a file?“, “C Programming – Dealing with files – How to write to the file?” and “C Programming – Dealing with files – How to fetch a record from the file?“.

Through this article, we will look into some of the un-buffered functions ‘C’ provides for handling files. Let’s start this article with the file open function.

open function

“open” is an un-buffered file management function used to open or create a file. The syntax of this function is:

int open(const char *pathname, int flags, mode_t mode);

Where “pathname” is the complete path of the file to open or create. “flags” controls the way to open or create a file; like open file in read-only (O_RDONLY) mode, open file in write-only mode (O_WRONLY), create a file (O_CREAT), etc. Flags can be bit-wise OR’d.

mode” tells the permissions to use if the new file is created. These flags are valid only if the O_CREAT flag is specified in the “flags” parameter. “mode” flags are also bit-wise OR’d.

Upon success, the “open” function will return the file descriptor of the file. Other file management functions will use this file descriptor to do file management activities. If the function fails, it returns the value “-1”.

Below is an example, the “open” function call to create a file and provide full file access permissions to the owner. If the file already exists, the file contents will be erased.

   int fd = open("dummyfile.tmp", O_CREAT | O_TRUNC, S_IRWXU);

close function

Once the file is opened, we should close the file. “close” function is used to close the previously opened file with the “open” function. This is an important function. If you don’t call “close” upon open files, those files may be locked and can not be accessed by any other processes. This will become a problem if multiple processes need to access the same file.

The syntax of the “close” function is very simple.

int close(int fd);

Where “fd” is the file descriptor of the opened file. Upon success “close” function returns “0”; otherwise it returns “-1”.

Putting altogether here is the simple code. It creates and opens the file “dummyfile.tmp”. Upon success, it closes the file.

// Sample.c
#include 

void main()
{
   int fd = open("dummyfile.tmp", O_CREAT | O_TRUNC, S_IRWXU);
   if ( fd > 0 )
   {
      close(fd);
   }
}

We will discuss more functions in the next articles…

// Malin

C Programming – Dealing with files – How to use open function to open a file?

2 thoughts on “C Programming – Dealing with files – How to use open function to open a file?

Leave a Reply

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

Scroll to top
Exit mobile version