Lab 8
Lab 8
OBJECTIVE
Learn to use open, read, write, close system for file management.
TIME REQUIRED : 3 hrs
PROGRAMMING LANGUAGE : C/C++
SOFTWARE REQUIRED : Ubuntu/Fedora, gcc/gc, Text Editor, Terminal, Windows,
Dev
HARDWARE REQUIRED : Core i5 in Computer Labs
FILE SYSTEM MANAGEMENT IN LINUX
File management system calls handle file manipulation jobs like creating a file, reading, and
writing, etc. The Linux System calls under this are open(), read(), write(), close().
open():
It is the system call to open a file.
This system call just opens the file, to perform operations such as read
and write, we need to execute different system call to perform the
operations.
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.
read():
This system call opens the file in reading mode
We can not edit the files with this system call.
Multiple processes can execute the read() system call on the same file
simultaneously.
Syntax:
length = read(file_descriptor , buffer, max_len);
Example:
n = read(0, buff, 50);
Here,
Return value: If successful read returns the number of bytes actually read.
write():
This system call opens the file in writing mode
We can edit the files with this system call.
Multiple processes can not execute the write() system call on the same
file simultaneously.
Syntax:
length = write(file_descriptor , buffer, len);
Example:
n = write(fd, "Hello world!", 12);
Here,
close():
This system call closes the opened file.
Syntax:
int close(int fd);
Here,
int main()
{
int n,fd;
char buff[50]; // declaring buffer
return 0;
}
Answer:
Activity 8.2
Write a program that creates a file with a 4K bytes free space. [2].
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
char buf1[]="LAB ";
char buf2[]="OS Linux";
int main( void)
{
int fd;
if ((fd=creat("file.gol", 0666)) < 0) {
printf("Creation error");
exit (1);
}
if (write(fd, buf1, sizeof(buf1)) < 0)
printf("Writing error");
exit(2);
}
if (lseek(fd, 4096, SEEK_SET) < 0)
printf("Positioning error");
exit(3);
}
if (write(fd, buf2, sizeof(buf2)) < 0)
printf("Writing error");
exit(2);
}
}
Trace the execution of the program with the help of the following commands:
ls -l
stat file.gol
od -c file.gol
Answer:
Activity 8.3:
The following code will open a file named “File1.txt” in writing mode. This will ask user to
enter numbers, it will read input from the terminal and save it in the file. Write down the
outcome of this code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
fptr = fopen("File1.txt","w");
if(fptr == NULL)
{
printf("Error");
exit(1);
}
printf("enter num");
scanf("%d",&num);
fprintf(fptr,"%d", num);
fclose(fptr);
}
Answer:
Activity 8.4:
The following code will open a file named “File1.txt” in read mode. Write down the
outcome of this code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
fscanf(fptr,"%d",&num);
}
Answer:
Review Questions:
1. What is the difference between open() and fopen() in Linux?
Bibliography
[1 A. Das, "File management system call in C programming," April 2019. [Online]. Available:
] https://www.includehelp.com/c/file-management-system-calls.aspx.
[2 "System calls for working with files and directories in Linux," [Online]. Available:
] https://profile.iiita.ac.in/bibhas.ghoshal/lab_files/System%20calls%20for%20files%20and
%20directories%20in%20Linux.html.