×

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

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 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.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

CRC Program in C

CRC (Cyclic Redundancy Check) is an error-detection algorithm that is used to detect any errors that may have occurred during the transmission or storage of data. The basic idea behind...

4 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

How to create a node in C?

A node is a small concept taken from the graph theory, or a node is a fundamental unit of a data structure, like a tree data structure. Every graph contains...

3 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.