Lab Manual 02
Lab Manual 02
Lab Manual 02
1
Install G++ the C++ Compiler on Ubuntu
The GNU Compiler Collection (GCC) is a collection of compilers and libraries for C, C++. Many
open-source projects, including the GNU tools and the Linux kernel, are compiled with GCC.
To able to add new repositories and install packages on your Ubuntu system, you must be logged
in as root or user with sudo privileges.
2
Basic C++ Program
let’s create hello world C++ program. Save the following code as hello.cpp text file and run it.
Perform the steps below.
1. Create a text file hello.cpp by following command and write simple code as shown in figure
$ nano hello.cpp
$ program-source-code.cpp -o executable-file-name
$ ./executable-file-name
3
I/O System Call
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.
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. 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:
Here,
4
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:
Here,
Return value: If successful read returns the number of bytes actually read.
3. write ()
Syntax:
Here,
Return value: If successful write() returns the number of bytes actually written.
5
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:
Here,
Example:
6
Lab Tasks