×

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.


Related Topics

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.