×

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

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

File Operations in C

Why do I need the file? All data will be lost when the program exits. Saving data to a file keeps it safe even if the program stops working. If there...

6 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.