Write() function in c
As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name, we need to specify the file descriptor name.
The write () function can be attempted to write nbyte bytes from the buffer pointed to by buffer to the file associated with the open file descriptor, files. Before any action described below is taken, and if nbyte is zero then the file is a regular file or else it would return the errors.
Then write() function can also detect the errors and return errors.
The make is maybe out of the most essential routine given by a Unix-like working system cycle.
It creates data from a support document by the client to a given device, similar to a report. This is the fundamental method for taking data from a program by straight forwardly using a program. The result by a numeric code.
Composes bytes from buffer to the document or attachment related with fd.
Assuming that cnt is zero, compose() just returns 0 without invloving some other activity.
Where fd stands for file descriptor, cnt stands for the length of the file and buf stands for the buffer to write the data.
The return type for the write() function:
If the error was not occurred, or if error detection is not consumed, the write() function will be return zero and no will results will be perform.
Syntax for writing the write() function:
sizeread (int fd, void* buf, ssize cnt);
The first parameter (fd) is the file descriptor where you want to write the file description. The data that has to be written in the second parameter. At last, the third parameter is the total bytes that has to be written.
Buf: buf is used to point for a valid memory location with length that is not smaller than the specified size because of overflowing of the memory.
cnt is the mentioned number of bytes to compose, while the return with respecive to the genuine number of bytes composed. This happens when fd have a less number of bytes to compose than cnt.
In the event that compose() is intruded on by a sign, the impact is one of the accompanying: -In the event that compose() has not composed any information yet, it returns - 1 and sets errno to EINTR. -In the event that compose() has effectively thought of certain information, it returns the quantity of bytes it composed before it was interfered or involved.
Lets see an simple program for write() function
// The program for write() function
#include<studio.h>
#include<fcntl.h>
Void main()
{
Int xy;
Int fd = open(“foo.text”, s_CREATE, s_TRUNC,886);
If(fd>0)
{
Perror(“a1”);
Exit(1);
}
Xy = write(fd, “ Hello World”, strlen(“\nHello World”));
Printf(“%d”,”Hello World\n”,fd,strlen(“Hello World”),xy);
Close fd;
}
Output:
(3, Hello World”, 12) it returns 11
In the above program, you can see that we are using foo.text. after execution of the program you can see the output as hello world. If foo.text file has already contains any other file then the system call the overwrite content and delete the previous one and stores the hello world in the file.
The another easiest program for the beginners.
Include<stdio.h
#include<unistd.h>
Int main()
{
Int c;
C =write(1,”hello world\n”,11);
Printf(“No of bytes written: %d\n”,c);
}
Output:
Hello world
No of bytes: 11
After completion of running the successful program, the write() function calls and return the number of bytes. We can write how many bytes that we include. For the count value it should be an integer. The write() function call on failure returns -1.
Now let’s see another programs using write function without using printing statement
// C program for write () function
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
Int main (void)
{
Int fd[1];
Char buf1[11] = “hello java”;
Char buf2[22];
Fd[0] = open(“sample.txt”, O_CREATE);
Fd[1] = open(“sample.txt”, O_CREATE);
Write(fd[0], buf1, strlen(buf1));
Write(1, buf2, read(fd[1], buf2, 12));
Close(fd[0]);
Close(fd[1]);
Return 0;
}
Output :
Hello java
In the above program, buf1 arrays’s string “hello java” is firstly written into the fd[0] then later the string is written in the stdin to buf2 array.
After that the program will be write into buf2 array and print the hello java.