Syntax
#include <io.h> int write(int handle, const void *buffer, unsigned int count);Description
write writes count bytes from buffer into the file associated with handle. The write operation begins at the current position of the file pointer associated with the given file. If the file is open for appending, the operation begins at the end of the file. After the write operation, the file pointer is increased by the number of bytes actually written.
If the given file was opened in text mode, each line feed character is replaced with a carriage return/line feed pair in the output. The replacement does not affect the return value.
write returns the number of bytes moved from the buffer to the file. The return value may be positive but less than count. A return value of -1 indicates an error, and errno is set to one of the following values: compact break=fit.
Value
This example writes the contents of the character array buffer to the output file whose handle is fh.
#include <io.h>#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <string.h> #define FILENAME "write.dat" int main(void) { int fh; char buffer[20]; memset(buffer, 'a', 20); /* initialize storage */ printf("\nCreating " FILENAME ".\n"); system("echo Sample Program > " FILENAME); if (-1 == (fh = open(FILENAME, O_RDWR|O_APPEND))) { perror("Unable to open " FILENAME); return EXIT_FAILURE; } if (5 != write(fh, buffer, 5)) { perror("Unable to write to " FILENAME); close(fh); return EXIT_FAILURE; } printf("Successfully appended 5 characters.\n"); close(fh); return 0; /**************************************************************************** The program should create a file "write.dat" containing: Sample Program aaaaa The output should be: Creating write.dat. Successfully appended 5 characters. ****************************************************************************/ }Related Information