C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C Prime Number code in C Factorial Program in C using For Loop Factorial Program in C Using While Loop Fibonacci Series in C Using For Loop Fibonacci series in C using while loop Prime Number Program in C using for Loop While Loop in C programming examples Built-in functions in C Assert() Function C vs Java Strings Call Back Function in Embedded C Else If Ladder fgets() function Ftell() Function getc() function getch() function gets() function Heap Sort Nested if-else statement Pi() Function Positioning of file Write() function abs() function in C Attributes in C C program to find factorial of a number using Recursion Ferror() in c fopen() function in C Fibonacci series program in C using Recursion Formatted Input and output function in C Snake Game in C User Defined Functions in C Beep() function in C Cbrt() function in C Hook() function in C Isalnum() function in C C Program to find the Roots of a Quadratic Equation C Switch Statements Difference between rand() and srand() function in C Difference between while and for loop in C Doubly Linked list in C Example of Iteration in C How to use atoi() function in C How to use floor() function in C How to use sine() function in C How to use Typedef Struct in C Integer Promotions in C C Program Swap Numbers in cyclic order Using Call by Reference C Program to Find Largest Number Using Dynamic Memory Allocation C Program to Find the Largest Number using Ternary Operator C/C++ Program to Find the Size of int, float, double and char Find the Largest Three Distinct Elements in an Array using C/C++ Loop Questions in C Modulus on Negative Numbers in C Multiplication table program in C using For loop Nested Loops in C Programming Examples C Program for Mean and Median of an Unsorted Array Results of Comparison Operations in C and C++ Reverse a Stack using Recursion in C Simple hash() function in C strcat() Function in C Sum of N numbers in C using For loop Use of free() function in C Write a program that produces different results in C and C++ C Function Argument and Return Values Keywords in C Bank management system in C Calendar application in C Floor() Function in C Free() Function in C How to delete a file in C How to move a text in C Remove an element from an array in C Unformatted input() and output() function in C What are linker and loader in C fork() in C GCD program in C Branching Statements in C Comma Operator in C Control statement in C Double Specifier in C How to create a binary file in C Long int in C Palindrome Number in C Pure Virtual Function in C Run Time Polymorphism in C Types of Array in C Types of Function in C What is a buffer in C What is required in each C Program Associativity of Operators in C Bit Stuffing Program in C Actual and Formal Parameters Addition of two Numbers in C Advantages of function in C Arithmetic Progression Program in C Binomial Coefficient Program in C Difference between Array and List in C Diffie-Hellman Algorithm in C How to convert a number to words in C How to convert a string to hexadecimal in C Difference between If and Switch Statement in C

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.



ADVERTISEMENT
ADVERTISEMENT