The write()
function is a low-level file manipulation function provided by the unistd.h
library in C, which performs write operations on a file.
Below we can see the syntax for the write()
function.
int write(int fileDescriptor, void *buffer, size_t bytesToWrite)
We need to provide the function with three arguments which are discussed below:
fileDescriptor
: It is an integer file descriptor for the opened file, which the open()
function returns when opening a file.
buffer
: This pointer points to a buffer containing the data we want to write into the file.
bytesToWrite
: Here, we provide an unsigned integer variable that specifies the maximum number of bytes we want to write from the buffer to the file.
When executed, the function returns an integer value of the number of bytes written to the file.
Note: To avoid buffer overflow, the max value of the
bytesToWrite
variable should not exceed the size of thebuffer
variable.
Below, we can see a C program that uses the write()
function to write the contents of the buffer to the file sample.txt
.
When we run the C program via the cat sample.txt command, we see that initially, the sample.txt
file is empty.
After running the program via the ./main
command, we again print the contents of the sample.txt
file. Now we see that it contains the data in the buffer variable.
Line 9: We make a character pointer and initialize it with the file name we want to open.
Line 11: We open the file via the open()
command in read and write mode specified by the second argument O_RDWR
.
Lines 13–19: Here, we check if the file opened correctly. If it does not, we exit the program with an exit status of 1
.
Line 21: We make a character pointer named buffer
and initialize it with the contents we want to write to the file.
Line 23: We call the write()
function to write the buffer
contents to the file sample.txt
. We pass three arguments: the file descriptor fd
, the char array buffer
, and the maximum number of bytes to write strlen(buffer)
. The function will return the number of bytes it wrote into the file, which we store in an integer variable bytesWritten
.
Line 25: Now, we will print the number of bytes that were written to the sample.txt
file.
Now that we understand how to write data to a file using the write()
function, let's test ourselves by completing the following code.
In the code file below, we have an buffer
object initialized with the string You are stronger than you think
and we have an empty text file named file.txt
.
Our task is to write 16 characters from the buffer to the file.
Complete the write()
function below to write You are stronger
into the file, which entails the first 16 bytes of the buffer.
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <string.h> int main(){ char* fileName = "file.txt"; int fd = open(fileName, O_RDWR); if(fd == -1){ printf("\nError Opening File!!\n"); exit(1); } else{ printf("\nFile %s opened successfully!\n", fileName); } char *buffer = "You are stronger than you think\n"; int bytesWritten = write(/*Write your code here*/); printf("%d bytes written successfully!\n", bytesWritten); return 0; }
The write()
function allows developers to write data to files by low-level system calls and ensure that the data is being written to the file due to its return value. It can be used to handle any file writing errors.