Home » C programming language

File management system calls in C programming

In this article, we are going to learn about the system calls for file management in UNIX/LINUX operating system and implementation of the systems calls.
Submitted by Anshuman Das, on April 24, 2019

The system call is a way for programs to interact with the operating system. When the program makes a system call at that time it makes a request to the operating system's kernel.

There are 5 different categories of system calls:

  1. Process Control
  2. File Management
  3. Device Management
  4. Information Management
  5. Communication

Here, we will discuss about the system calls for file management in Unix system,

There are four system calls for file management,

  1. open ()
  2. read ()
  3. write ()
  4. close ()

1. open ()

open() system call is used to know the file descriptor of user-created files. Since read and write use file descriptor as their 1st parameter so to know the file descriptor open() system call is used.

Syntax:

    fd = open (file_name, mode, permission);
    Example:
    fd = open ("file", O_CREAT | O_RDWR, 0777);

Here,

  • file_name is the name to the file to open.
  • mode is used to define the file opening modes such as create, read, write modes.
  • permission is used to define the file permissions.

Return value: Function returns the file descriptor.

2. read ()

read() system call is used to read the content from the file. It can also be used to read the input from the keyboard by specifying the 0 as file descriptor (see in the program given below).

Syntax:

    length = read(file_descriptor , buffer, max_len);
    Example:
    n = read(0, buff, 50);

Here,

  • file_descriptor is the file descriptor of the file.
  • buffer is the name of the buffer where data is to be stored.
  • max_len is the number specifying the maximum amount of that data can be read

Return value: If successful read returns the number of bytes actually read.

3. write ()

write() system call is used to write the content to the file.

Syntax:

    length = write(file_descriptor , buffer, len);
    Example:
    n = write(fd, "Hello world!", 12);

Here,

  • file_descriptor is the file descriptor of the file.
  • buffer is the name of the buffer to be stored.
  • len is the length of the data to be written.

Return value: If successful write() returns the number of bytes actually written.

4. close ()

close() system call is used to close the opened file, it tells the operating system that you are done with the file and close the file.

Syntax:

    int close(int fd);

Here,

  • fd is the file descriptor of the file to be closed.

Return value: If file closed successfully it returns 0, else it returns -1.

C code to demonstrate example of system calls

#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>

int main()
{
	int n,fd;
	char buff[50];	// declaring buffer 
	
	//message printing on the display
	printf("Enter text to write in the file:\n");
	//read from keyboard, specifying 0 as fd for std input device
	//Here, n stores the number of characters	
	n= read(0, buff, 50);

	// creating a new file using open.
	fd=open("file",O_CREAT | O_RDWR, 0777); 
	
	//writting input data to file (fd)
	write(fd, buff, n);
	//Write to display (1 is standard fd for output device)
	write(1, buff, n);

	//closing the file
	int close(int fd);
	
	return 0;	
}

Output

Enter text to write in the file:
Hello world, welcome @ IncludeHelp
Hello world, welcome @ IncludeHelp





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.