Linux Program to Open and Close a File using File Descriptors

This Linux Program illustrates Opening and Closing a File using File Descriptors. We will going to use open() system call to open the file that takes two arguments. First argument is the file-name and second must include one of the O_RDONLY, O_WRONLY, or O_RDWR. O_RDONLY indicates open the file in read-only mode, with this mode user would not be able to write to file. O_WRONLY indicates open the file in write-only mode, with this mode user would not be able to read from the file. O_RDWR indicates open the file in both read and write mode. In this example, we are going to use O_RDWR mode. The open() system call returns an integer value, which is called the file descriptor and it is uniquely associated with each file that we opens. File descriptor can further be used to perform read, write and close operations. Now, to close the file we use close() system call that takes the file descriptor of the file that we would like to close.

Here is the source code of Linux Program to Open and Close a File using File Descriptors. The Linux Program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * Linux Program to Open and Close a File using File Descriptors.
  3.  */
  4.  
  5. #include 
  6. #include 
  7. #include 
  8. #include 
  9. #include 
  10. #include 
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.     int fd;
  15.  
  16.     if (argc = 0 ) {
  17.         printf("File %s opened successfully.\n", argv[1]);
  18.     } else {
  19.         perror("Problem in opening the file");
  20.         exit(1);
  21.     }
  22.  
  23.     printf("Time to close the opened file %s.\n", argv[1]);
  24.     /* closing the opened file */
  25.     close(fd);
  26. }

$ gcc open_close_file.c
$ ./a.out my_file
 
This example would give error if the file
does not exist and need to first create 
a new file. In this example we are assuming
that the file already exist and we will see
the output,
 
File my_file opened successfully.
Time to close the opened file my_file.

Sanfoundry Global Education & Learning Series – 1000 Linux Programs.

advertisement
advertisement
If you wish to look at all Linux Programming examples, go to Linux Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.