×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

File management system calls in C programming

In this tutorial, we will learn about the system calls for file management in UNIX/LINUX operating system and implementation of the systems calls. By Anshuman Das Last updated : April 20, 2023

What is a system calls in C?

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.

Categories of system calls

There are 5 different categories of system calls:

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

System calls for file management

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 program to demonstrate the 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!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.