write(2, "A write error has occurred on file descriptor 1\n",46);
exit(0);
}
read 系统调用
1
2
3
#include <unistd>
size_t read(int fildes,void *buf, size_t nbytes);
从与文件相关联的文件读入nbytes个字节的数据,并把他们放入到数据区buf中。
如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<unistd.h>
#include<stdlib.h>
intmain()
{
char buffer[128];
int nread;
nread = read(0, buffer, 128);
if (nread == -1)
write(2, "A read error has occurred\n", 26);
if ((write(1,buffer,nread)) != nread)
write(2, "A write error has occurred\n",27);
exit(0);
}
open
To create a new file descriptor, you need to use the open system call.
1
2
3
4
5
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
intopen(constchar *path, int oflags);
intopen(constchar *path, int oflags, mode_t mode);
In simple terms, open establishes an access path to a file or device. If successful, it returns a file descriptor that can be used in read , write , and other system calls.
For example,
1
open (“myfile”, O_CREAT, S_IRUSR|S_IXOTH);
close
You use close to terminate the association between a file descriptor, fildes , and its file. The file descriptor becomes available for reuse. It returns 0 if successful and –1 on error.
1
2
#include<unistd.h>
intclose(int fildes);
ioctl
It provides an interface for controlling the behavior of devices and their descriptors and configuring underlying services.
1
2
#include<unistd.h>
intioctl(int fildes, int cmd, ...);
For example, the following call to ioctl on Linux turns on the keyboard LEDs:
fopen opens the file named by the filename parameter and associates a stream with it. The mode parameter specifies how the file is to be opened. It’s one of the following strings:
“r” or “rb” : Open for reading only
“w” or “wb” : Open for writing, truncate to zero length
“a” or “ab” : Open for writing, append to end of file
“r+” or “rb+” or “r+b” : Open for update (reading and writing)
“w+” or “wb+” or “w+b” : Open for update, truncate to zero length
“a+” or “ab+” or “a+b” : Open for update, append to end of file
The fread library function is used to read data from a file stream. Data is read into a data buffer given by ptr from the stream, stream . Both fread and fwrite deal with data records. These are specified by a record size, size , and a count, nitems , of records to transfer. The function returns the number of items (rather than the number of bytes) successfully read into the data buffer. At the end of a file, fewer than nitems may be returned, including zero.
The fwrite library call has a similar interface to fread . It takes data records from the specified data buffer and writes them to the output stream. It returns the number of records successfully written.
fcolse
1
2
#include<stdio.h>
intfclose(FILE *stream);
The fclose library function closes the specified stream , causing any unwritten data to be written.
fflush
1
2
#include<stdio.h>
intfflush(FILE *stream);
The fflush library function causes all outstanding data on a file stream to be written immediately.
fseek
1
2
#include<stdio.h>
intfseek(FILE *stream, longint offset, int whence);
fgetc, getc, and getchar
1
2
3
4
#include<stdio.h>
intfgetc(FILE *stream);
intgetc(FILE *stream);
intgetchar();
fgets and gets
1
2
3
#include<stdio.h>
char *fgets(char *s, int n, FILE *stream);
char *gets(char *s);
The fgets function reads a string from an input file stream .
fcntl(fildes, F_DUPFD, newfd) : This call returns a new file descriptor with a numerical value equal to or greater than the integer newfd . The new descriptor is a copy of the descriptor fildes . Depending on the number of open files and the value of newfd , this can be effectively the same as dup(fildes) .
fcntl(fildes, F_GETFD) : This call returns the file descriptor flags as defined in fcntl.h . These include FD_CLOEXEC , which determines whether the file descriptor is closed after a suc- cessful call to one of the exec family of system calls.
fcntl(fildes, F_SETFD, flags) : This call is used to set the file descriptor flags, usually just FD_CLOEXEC .
fcntl(fildes, F_GETFL) and fcntl(fildes, F_SETFL, flags) : These calls are used, respectively, to get and set the file status flags and access modes. You can extract the file access modes by using the mask O_ACCMODE defined in fcntl.h . Other flags include those passed in a third argument to open when used with O_CREAT . Note that you can’t set all flags. In particular, you can’t set file permissions using fcntl .
mmap
1
2
#include<sys/mman.h>
void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);