{"id":3685,"date":"2024-03-06T11:41:39","date_gmt":"2024-03-06T11:41:39","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3685"},"modified":"2024-03-06T11:41:39","modified_gmt":"2024-03-06T11:41:39","slug":"input-output-system-calls-in-c-create-open-close-read-write","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/input-output-system-calls-in-c-create-open-close-read-write\/","title":{"rendered":"Input-output system calls in C | Create, Open, Close, Read, Write"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#input-output-system-calls-in-c-create-open-close-read-write\">Input-output system calls in C | Create, Open, Close, Read, Write<\/a><\/li><li ><a href=\"#important-terminology\">Important Terminology<\/a><ul><li ><a href=\"#what-is-the-file-descriptor\">What is the File Descriptor?<\/a><\/li><\/ul><\/li><li ><a href=\"#input-output-system-calls\">Input\/Output System Calls<\/a><\/li><li ><a href=\"#1-create\">1. Create<\/a><ul><li ><a href=\"#how-c-create-works-in-os\">How C create() works in OS<\/a><\/li><\/ul><\/li><li ><a href=\"#2-c-open\">2. C open<\/a><ul><li ><a href=\"#how-c-open-works-in-os\">How C open() works in OS<\/a><\/li><li ><a href=\"#example-of-c-open\">Example of C open()<\/a><\/li><\/ul><\/li><li ><a href=\"#3-c-close\">3. C close<\/a><ul><li ><a href=\"#how-c-close-works-in-the-os\">How C close() works in the OS<\/a><\/li><li ><a href=\"#example-1-close-in-c\">Example 1: close() in C<\/a><\/li><li ><a href=\"#example-2\">Example 2<\/a><\/li><\/ul><\/li><li ><a href=\"#4-c-read\">4. C read<\/a><ul><li ><a href=\"#parameters\">Parameters<\/a><\/li><\/ul><\/li><li ><a href=\"#5-c-write\">5. C write<\/a><\/li><li ><a href=\"#important-points-about-c-write\">Important Points about C write<\/a><ul><li ><a href=\"#example-of-write-in-c\">Example of write() in C<\/a><\/li><\/ul><\/li><li ><a href=\"#example-print-hello-world-from-the-program-without-using-any-printf-function\">Example: Print \u201chello world\u201d from the program without using any printf function.<\/a><\/li><li ><a href=\"#faq-input-output-system-calls-in-c-create-open-close-read-write\">FAQ- Input-output system calls in C | Create, Open, Close, Read, Write<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"input-output-system-calls-in-c-create-open-close-read-write\">Input-output system calls in C | Create, Open, Close, Read, Write<\/h2>\n\n\n\n<p>System calls in C are requests made by a program to the operating system kernel for services it can&#8217;t access directly. These services go beyond input and output devices and include functions like process control, file management, memory management, and inter-process communication. In C programming, various functions, such as create, open, read, and write, are used for input\/output system calls.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"important-terminology\">Important Terminology<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"what-is-the-file-descriptor\">What is the File Descriptor?<\/h3>\n\n\n\n<p>The file descriptor is basically an integer that would identify an open file of the process.<\/p>\n\n\n\n<p>A <strong>file descriptor table<\/strong> is a collection of integer array indices where each index corresponds to a file descriptor. Each element in this table is a pointer to a file table entry. In an operating system, there is one unique file descriptor table for each process.<\/p>\n\n\n\n<p>A <strong>file table entry<\/strong> is an in-memory structure representing an open file. It is created when a program requests to open a file. These entries maintain information about the file, including the current file position.<\/p>\n\n\n\n<p><strong>Standard File Descriptors:<\/strong> When a process starts, it automatically has three standard file descriptors open: 0, 1, and 2. These are often referred to as stdin (0), stdout (1), and stderr (2). By default, each of these descriptors points to a file table entry for a file named <code>\/dev\/tty<\/code>.<\/p>\n\n\n\n<p><strong>\/dev\/tty:<\/strong> This is an in-memory surrogate for the terminal. In other words, it&#8217;s a representation in the computer&#8217;s memory that stands for the terminal device. The terminal, in this context, is a combination of a keyboard and a video screen where the user interacts with the system.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Read from stdin (fd 0):<\/strong><br>When you type on the keyboard, the program reads from standard input (stdin), linked to file descriptor 0, and saves it from the file named <code>\/dev\/tty<\/code>.<\/li>\n\n\n\n<li><strong>Write to stdout (fd 1):<\/strong><br>Output you see on the screen is written to standard output (stdout), associated with file descriptor 1, and comes from the file named <code>\/dev\/tty<\/code>.<\/li>\n\n\n\n<li><strong>Write to stderr (fd 2):<\/strong><br>Error messages you see on the screen are written to standard error (stderr), linked to file descriptor 2, and also come from the file named <code>\/dev\/tty<\/code>.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"input-output-system-calls\"><strong>Input\/Output System Calls<\/strong><\/h2>\n\n\n\n<p>There are 5 Input\/Output system calls.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-create\">1. Create<\/h2>\n\n\n\n<p>The <code>creat()<\/code> function in C is used to create a new empty file. It allows us to specify the permissions and the name of the file we want to create. This function is defined in the <code>&lt;unistd.h&gt;<\/code> header file, and the flags used as arguments are defined in the <code>&lt;fcntl.h&gt;<\/code> header file. The <code>creat()<\/code> function is a system call for file creation in C programming.<\/p>\n\n\n\n<p><strong>Syntax of create() in C<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int create(char *filename, mode_t mode);\n<\/code><\/pre>\n\n\n\n<p>Parameter<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>filename:<\/strong>It refers to the &nbsp;name of the file which you want to create<\/li>\n\n\n\n<li><strong>mode:<\/strong>&nbsp;It is the permissions of the new file.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Return Value<\/h4>\n\n\n\n<p>When creating or opening a file, the first unused file descriptor is often returned, starting with 3 (as 0, 1, and 2 are typically reserved for stdin, stdout, and stderr). If there&#8217;s an error, the function returns -1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-c-create-works-in-os\"><strong>How C create() works in OS<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Create a new empty file on the disk.<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use a file creation function, such as <code>creat()<\/code> or <code>open()<\/code> with appropriate flags, to create a new file on the disk.<\/li>\n<\/ul>\n\n\n\n<p>2.<strong>Create a file table entry.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The operating system creates an in-memory structure called a file table entry to represent the newly created file. This structure holds information about the file, such as the current file position.<\/li>\n<\/ul>\n\n\n\n<p>3.<strong>Set the first unused file descriptor to point to the file table entry.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The operating system assigns the next available file descriptor (starting from 3, as 0, 1, and 2 are often reserved for stdin, stdout, and stderr) to the newly created file. This file descriptor now points to the file table entry in memory.<\/li>\n<\/ul>\n\n\n\n<p>4.<strong>Return the file descriptor used, -1 upon failure.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the file creation and descriptor assignment are successful, return the newly assigned file descriptor. Otherwise, return -1 to indicate an error.<\/li>\n<\/ul>\n\n\n\n<p>In C, this process might involve using functions like <code>creat()<\/code>, <code>open()<\/code>, and checking the return values for success or failure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-c-open\"><strong>2. C open<\/strong><\/h2>\n\n\n\n<p>The <code>open()<\/code> function in C is used to open a file for reading, writing, or both. It is also capable of creating the file if it does not exist. This function is defined in the <code>&lt;unistd.h&gt;<\/code> header file, and the flags passed as arguments are defined in the <code>&lt;fcntl.h&gt;<\/code> header file. When using <code>open()<\/code>, you can specify flags to control the file opening mode, and it&#8217;s important to note that additional flags are needed to create a file if it doesn&#8217;t exist.<\/p>\n\n\n\n<p><strong>Syntax of open() in C<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int open (const char* Path, int flags);\n\n\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Path:<\/strong> Specify the path to the file you want to open. Use an absolute path (starting with &#8220;\/&#8221;) when not working in the same directory as the C source file. Use a relative path (just the file name with extension) when working in the same directory.<\/li>\n\n\n\n<li><strong>Flags:<\/strong> Flags are used to specify how you want to open the file. You can choose from various flags based on your requirements.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Flags<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><strong>O_RDONLY<\/strong><\/td><td>It will Open the file in read-only mode.<\/td><\/tr><tr><td><strong>O_WRONLY<\/strong><\/td><td>It will Open the file in write-only mode.<\/td><\/tr><tr><td><strong>O_RDWR<\/strong><\/td><td>It will Open the file in read and write mode.<\/td><\/tr><tr><td><strong>O_CREAT<\/strong><\/td><td>It can Create a file if it doesn\u2019t exist.<\/td><\/tr><tr><td><strong>O_EXCL<\/strong><\/td><td>It can Prevent creation if it already exists.<\/td><\/tr><tr><td><strong>O_ APPEND<\/strong><\/td><td>It will Open the file and places the cursor at the end of the contents.<\/td><\/tr><tr><td><strong>O_ASYNC<\/strong><\/td><td>It will Enable input and output control by signal.<\/td><\/tr><tr><td><strong>O_CLOEXEC<\/strong><br><\/td><td>It will Enable close-on-exec mode on the open file.<br><br><\/td><\/tr><tr><td><strong>O_NONBLOCK<\/strong><br><\/td><td>It will Disable the  blocking of the file opened.<br><br><\/td><\/tr><tr><td><strong>O_TMPFILE<\/strong><br><\/td><td>Create an unnamed temporary file at the specified path.<br><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-c-open-works-in-os\"><strong>How C open() works in OS<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Find the existing file on the disk.<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This involves searching for and identifying the file you want to access. You can use the file&#8217;s path, either absolute or relative, depending on your current working directory.<\/li>\n<\/ul>\n\n\n\n<p>2.<strong>Create a file table entry.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The operating system creates an in-memory structure called a file table entry to represent the existing file. This structure contains information about the file, such as the current file position.<\/li>\n<\/ul>\n\n\n\n<p>3.<strong>Set the first unused file descriptor to point to the file table entry.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The operating system assigns the next available file descriptor (typically starting from 3) to the existing file. This file descriptor now points to the file table entry in memory.<\/li>\n<\/ul>\n\n\n\n<p>4.<strong>Return the file descriptor used, -1 upon failure.<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the file is successfully opened and a file descriptor is assigned, return the file descriptor. Otherwise, return -1 to indicate an error.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-c-open\">Example of C open()<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/ C program to illustrate \n\/\/ open system call \n#include &lt;errno.h&gt; \n#include &lt;fcntl.h&gt; \n#include &lt;stdio.h&gt; \n#include &lt;unistd.h&gt; \n  \nextern int errno; \n  \nint main() \n{ \n    \/\/ if file does not have in directory \n    \/\/ then file foo.txt is created. \n    int fd = open(\"foo.txt\", O_RDONLY | O_CREAT); \n  \n    printf(\"fd = %d\\n\", fd); \n  \n    if (fd == -1) { \n        \/\/ print which type of error have in a code \n        printf(\"Error Number % d\\n\", errno); \n  \n        \/\/ print program detail \"Success or failure\" \n        perror(\"Program\"); \n    } \n    return 0; \n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fd = 3\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-c-close\"><strong>3. C close<\/strong><\/h2>\n\n\n\n<p>The <code>close()<\/code> function in C is used to inform the operating system that you have finished using a file descriptor and want to close the associated file. This function is defined in the <code>&lt;unistd.h&gt;<\/code> header file.<\/p>\n\n\n\n<p>Syntax of close() in C<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int close(int fd);\n<\/code><\/pre>\n\n\n\n<p>Parameter<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>fd: <\/strong>It represents the File descriptor of the file that you want to close.<\/li>\n<\/ul>\n\n\n\n<p>Return Value<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>0<\/strong>&nbsp;on success.<\/li>\n\n\n\n<li><strong>-1<\/strong>&nbsp;on error.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"how-c-close-works-in-the-os\"><strong>How C close() works in the OS<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Destroy file table entry referenced by element fd:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Ensure that no other process is pointing to the file table entry.<\/li>\n\n\n\n<li>Remove or destroy the file table entry associated with the specified file descriptor (fd).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Set element fd of the file descriptor table to NULL:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Update the file descriptor table, setting the entry corresponding to fd to NULL. This indicates that the file descriptor is no longer associated with any file.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1-close-in-c\"><strong>Example 1: close() in C<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate close system Call \n#include &lt;fcntl.h&gt; \n#include &lt;stdio.h&gt; \n#include &lt;unistd.h&gt; \n  \nint main() \n{ \n    int fd1 = open(\"foo.txt\", O_RDONLY); \n    if (fd1 &lt; 0) { \n        perror(\"c1\"); \n        exit(1); \n    } \n    printf(\"opened the fd = % d\\n\", fd1); \n  \n    \/\/ Using close system Call \n    if (close(fd1) &lt; 0) { \n        perror(\"c1\"); \n        exit(1); \n    } \n    printf(\"closed the fd.\\n\"); \n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>opened the fd = 3\nclosed the fd.\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2\">Example 2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate close system Call  \n#include&lt;stdio.h&gt;  \n#include&lt;fcntl.h&gt;  \nint main()  \n{  \n    \/\/ assume that foo.txt is already created  \n    int fd1 = open(\"foo.txt\", O_RDONLY, 0);  \n    close(fd1);  \n      \n    \/\/ assume that baz.tzt is already created  \n    int fd2 = open(\"baz.txt\", O_RDONLY, 0);  \n      \n    printf(\"fd2 = % d\\n\", fd2);  \n    exit(0);  \n}  <\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fd2 = 3\n<\/code><\/pre>\n\n\n\n<p>When the program starts, file descriptors 0, 1, and 2 are already used. So, the first available file descriptor is 3. After closing and setting them to NULL, the next <code>open()<\/code> gets file descriptor 3. That&#8217;s why the output of this program is 3.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-c-read\"><strong>4. C read<\/strong><\/h2>\n\n\n\n<p>The <code>read()<\/code> function in C reads a specified number of bytes (<code>cnt<\/code>) from the file associated with the file descriptor (<code>fd<\/code>) into the memory area pointed to by <code>buf<\/code>. A successful <code>read()<\/code> also updates the access time for the file. This function is defined in the <code>&lt;unistd.h&gt;<\/code> header file.<\/p>\n\n\n\n<p>Syntax of read() in C<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>size_t read (int fd, void* buf, size_t cnt);\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"parameters\"><strong>Parameters<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>fd:<\/strong> This is the file descriptor of the file from which you want to read data.<\/li>\n\n\n\n<li><strong>buf:<\/strong> The buffer is where the data will be read into. It&#8217;s a designated memory area.<\/li>\n\n\n\n<li><strong>cnt:<\/strong> This represents the length of the buffer, specifying how much data you want to read.<\/li>\n<\/ul>\n\n\n\n<p>Important Points<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>buf:<\/strong> Make sure that <code>buf<\/code> points to a safe memory location and has enough space (at least as much as <code>cnt<\/code>) to avoid any problems.<\/li>\n\n\n\n<li><strong>fd:<\/strong> Use a valid file descriptor from <code>open()<\/code>. If <code>fd<\/code> is not valid (NULL), reading will cause an error.<\/li>\n\n\n\n<li><strong>cnt:<\/strong> This is how many bytes you want to read. But, keep in mind, the actual number of bytes read may be less than what you asked for<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;unistd.h&gt; \n  \nint main() \n{ \n    int fd, sz; \n    char* c = (char*)calloc(100, sizeof(char)); \n  \n    fd = open(\"foo.txt\", O_RDONLY); \n    if (fd &lt; 0) { \n        perror(\"r1\"); \n        exit(1); \n    } \n  \n    sz = read(fd, c, 10); \n    printf(\"called read(% d, c, 10). returned that\"\n           \" %d bytes were read.\\n\", \n           fd, sz); \n    c&#91;sz] = '\\0'; \n    printf(\"Those bytes are as follows: % s\\n\", c); \n  \n    return 0; \n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>called read(3, c, 10).  returned that 10 bytes  were read.\nThose bytes are as follows: 0 0 0 foo.<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-c-write\">5. C write<\/h2>\n\n\n\n<p>Writes cnt bytes from buf to the file or socket associated with fd. cnt should not be greater than INT_MAX (defined in the limits.h header file). If cnt is zero, write() simply returns 0 without attempting any other action. The write() is also defined inside &lt;unistd.h&gt; header file.<\/p>\n\n\n\n<p><strong>Syntax of write() in C<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>size_t write (int fd, void* buf, size_t cnt); \n<\/code><\/pre>\n\n\n\n<p><strong>Parameters<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>fd:<\/strong>&nbsp;It is referred as the file descriptor<\/li>\n\n\n\n<li><strong>buf:<\/strong>&nbsp;It is the buffer to write data to.<\/li>\n\n\n\n<li><strong>cnt:<\/strong>&nbsp;It is the length of the buffer.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return Value<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It will returns the number of bytes written on success.<\/li>\n\n\n\n<li>It will return 0 on reaching the End of File.<\/li>\n\n\n\n<li>return -1 on error.<\/li>\n\n\n\n<li>return -1 on signal interrupts<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"important-points-about-c-write\"><strong>Important Points about C write<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>File Open:<\/strong> Make sure the file is opened for writing before using <code>write()<\/code>.<\/li>\n\n\n\n<li><strong>Buffer Size:<\/strong> Ensure that the size of <code>buf<\/code> is at least as large as specified by <code>cnt<\/code> to prevent overflow.<\/li>\n\n\n\n<li><strong>Writing Bytes:<\/strong> <code>cnt<\/code> is the number of bytes you want to write. The return value is the actual number of bytes written, which might be less than <code>cnt<\/code> if there are fewer bytes available.<\/li>\n\n\n\n<li><strong>Signal Interruption:<\/strong> If a signal interrupts the <code>write()<\/code>:\n<ul class=\"wp-block-list\">\n<li>If no data is written, it returns -1 and sets an error code.<\/li>\n\n\n\n<li>If some data is written before the interruption, it returns the number of bytes written before being interrupted.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-of-write-in-c\">Example of write() in C<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate  \n\/\/ write system Call  \n#include&lt;stdio.h&gt;  \n#include &lt;fcntl.h&gt;  \nmain()  \n{  \nint sz;  \n  \nint fd = open(\"foo.txt\", O_WRONLY | O_CREAT | O_TRUNC, 0644);  \nif (fd &lt; 0)  \n{  \n    perror(\"r1\");  \n    exit(1);  \n}  \n  \nsz = write(fd, \"hello skillvertex\\n\", strlen(\"hello skillvertex\\n\"));  \n  \nprintf(\"called write(% d, \\\"hello skillvertex\\\\n\\\", %d).\"\n    \" It returned %d\\n\", fd, strlen(\"hello skillvertex\\n\"), sz);  \n  close(fd);  \n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>called write(3, \"hello skillvertex\\n\", 12).  it returned 11\n<\/code><\/pre>\n\n\n\n<p>Hence, we can view  the file foo.txt after running the code, you will get output  a \u201c<em>hello <\/em>skillvertex\u201c. If foo.txt file already contains some content in it then the write a system call that can  overwrite the content and all previous content will be <em><strong>deleted<\/strong><\/em>&nbsp;and only \u201c<em>hello <\/em>skillvetex\u201d content will have in the file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-print-hello-world-from-the-program-without-using-any-printf-function\"><strong>Example: Print \u201chello world\u201d from the program without using any printf function.<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to illustrate \n\/\/ I\/O system Calls \n#include &lt;fcntl.h&gt; \n#include &lt;stdio.h&gt; \n#include &lt;string.h&gt; \n#include &lt;unistd.h&gt; \n  \nint main(void) \n{ \n    int fd&#91;2]; \n    char buf1&#91;12] = \"hello world\"; \n    char buf2&#91;12]; \n  \n    \/\/ assume foobar.txt is already created \n    fd&#91;0] = open(\"foobar.txt\", O_RDWR); \n    fd&#91;1] = open(\"foobar.txt\", O_RDWR); \n  \n    write(fd&#91;0], buf1, strlen(buf1)); \n    write(1, buf2, read(fd&#91;1], buf2, 12)); \n  \n    close(fd&#91;0]); \n    close(fd&#91;1]); \n  \n    return 0; \n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hello world<\/code><\/pre>\n\n\n\n<p>Whereas, buf1 array\u2019s string&nbsp; will print <em><strong>\u201chello world\u201d<\/strong><\/em>&nbsp;is first written into stdin fd[0] then after that this string will  write into stdin to buf2 array. After that write into buf2 array to the stdout and print output \u201c<em><strong>hello world<\/strong><\/em>\u201c.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-input-output-system-calls-in-c-create-open-close-read-write\">FAQ- Input-output system calls in C | Create, Open, Close, Read, Write<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1700653698105\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How to create open close read and write files in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. fopen() &#8211;  It will create a new file or open a existing file.<br \/>fclose() &#8211; f close() will close a file.<br \/>getc() &#8211; It can reads a character from a file.<br \/>putc() &#8211; It will writes a character to a file.<br \/>fscanf() &#8211; It will reads a set of data from a file.<br \/>fprintf() &#8211; It will writes a set of data to a file.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700653703283\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What does write () do in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <code>write()<\/code> attempts to write a specified number of bytes from a buffer to a file associated with an open file descriptor.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700653711559\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is input and output in C language?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C, you can use input and output functions to work with strings. Two common functions are <code>gets()<\/code> for reading strings and <code>puts()<\/code> for writing strings in an unformatted way.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Input-output system calls in C | Create, Open, Close, Read, Write System calls in C are requests made by a program to the operating system kernel for services it can&#8217;t access directly. These services go beyond input and output devices and include functions like process control, file management, memory management, and inter-process communication. In C &#8230; <a title=\"Input-output system calls in C | Create, Open, Close, Read, Write\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/input-output-system-calls-in-c-create-open-close-read-write\/\" aria-label=\"More on Input-output system calls in C | Create, Open, Close, Read, Write\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":3687,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[608,606,607,609],"class_list":["post-3685","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-close","tag-input-output-system-calls-in-c-create","tag-open","tag-read","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3685"}],"version-history":[{"count":9,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3685\/revisions"}],"predecessor-version":[{"id":7956,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3685\/revisions\/7956"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/3687"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}