1.
Read files that are already open
#include <unistd.h>ssize_t read (int fd,void *buf,size_t count);
Returns the number of bytes read, if it has returned 0 at the bottom of the file, an error returned-1
BUF is a pointer to a buffer that holds the data that will be read to the terminal, and count indicates the number of bytes to be read
2.
Write data to an open file
#include <unistd.h>ssize_t write (int fd,void *buf,size_t count);
Returns the number of bytes already written, with an error of 1
If you specify the O_append option, the end of the file is positioned before each write
Example:
1#include <stdio.h>2#include <sys/types.h>3#include <sys/stat.h>4#include <fcntl.h>5#include <unistd.h>6#include <string.h>7#include <stdlib.h>8 9 #defineFILENAME "Hello"Ten #defineBuf_size 80 One #defineFLAGS o_rdwr| O_append A intMainvoid) - { - intcount; the intFD; - CharWrite_buff[buf_size]; - Const Char*pathname=FILENAME; - if((Fd=open (pathname,flags)) ==-1) + { -printf"Error"); +Exit1); A } atprintf"Ok,has opened file\n"); -printf"start to write:\n"); - -scanf"%[^\n]", Write_buff); - -Count=strlen (write_buff); in if(Write (Fd,write_buff,count) ==-1) - { toprintf"error to write\n"); +Exit0); - } theprintf"OK have write to file\n"); * return 0; $}
Read/write files for file I/O operations-based on Linux system file IO