一、系统调用方式

1.文件描述符

内核利用文件描述符来访问文件,文件描述符是非负整数。打开现存文件或新建文件时,内核会返回一个文件描述符。

 
2.int open(const char *pathname, int flags)

 int open(cosnt char *pathname, int flags, mode_t mode) 

描述:Given a pathname for a file, open() returns a file descriptor, a small, nonnegative integer for use in subsequent system calls (read(2),write(2), lseek(2), fcntl(2), etc.). The file descriptor returned by a successful call will be the lowest-numbered file descriptor not cur‐rently open for the process.

成功:返回一个文件描述符

失败:返回-1

flags:
O_APPEND:追加方式,Before each write ,the file offset is positioned at the end of  the file, as if with lseek.  
O_CREAT:文件不存在时,创建该文件。当使用该标志时,就必须使用mode参数,记录待创建文件的访问权限。

 

3.int create(const char *pathname, mode_t mode)

描述:创建一个文件并以只写的方式打开文件,等价于:open 使用 flag:O_CREAT|O_WRONLY|O_TRUNC 

成功:返回对应的文件描述符。

失败:返回-1

 

4.ssize_t read(int fd, void *buf, size_t count)

描述:read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.

成功:返回实际读到的字节数(zero indicates end of file), and the file position is advanced by this number.

失败:返回-1

 
5.ssize_t write(int fd, const void *buf, size_t count);

描述:write() writes up to count bytes from the buffer pointed buf to the file referred to by the file descriptor fd.

成功:返回实际写入的字节数 (zero indicates nothing was written)

失败:返回-1

重要:文件指针(file position),随着操作往后移。

  

6.off_t lseek(int fd, off_t offset, int whence);

描述:The lseek() function repositions the offset of the open file associated with the file descriptor fd to the argument offset according to the

directive whence as follows:

SEEK_SET
The offset is set to offset bytes.

SEEK_CUR
The offset is set to its current location plus offset bytes.

SEEK_END
The offset is set to the size of the file plus offset bytes.(此时,offset一般小于0)

成功:返回移动后文件指针距离文件头的偏移

失败:返回-1

   

7.int dup(int oldfd);

描述:This system calls create a copy of the file descriptor oldfd. dup() uses the lowest-numbered unused descriptor for the new descrip‐

tor.

成功:返回新的文件描述符
失败:返回-1

   

8.int close(int fd);

描述:close a file descriptor.

成功:返回0

失败:返回-1

 

二、库函数调用方式

优势:使用库函数进行程序设计可提高程序的可移植性


1.流概念

 
2.文件指针 FILE *fp

FILE是由系统定义的一个结构,该结构中含有文件名、文件状态和文件当前位置等信息。通过文件指针fp就可对它所指的文件进行各种操作。

 

3.FILE *fopen(const char *path, const char *mode);

描述:The fopen() function opens the file whose name is the string pointed to by path and associates a stream with it.

The argument mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.):

r :Open text file for reading. The stream is positioned at the beginning of the file.

r+:Open for reading and writing. The stream is positioned at the beginning of the file.

w :Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.

w+:Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

a :Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

a+:Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

成功:返回文件指针
失败:返回NULL

 

4.size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

描述:The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.

成功:return the number of items successfully read(i.e., not the number of characters).

失败:a short item count (or zero).fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.

 

5.size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

描述:The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the loca‐tion given by ptr.

成功:return the number of items successfully written (i.e., not the number of characters).

失败:a short item count (or zero).

 

6.int fseek(FILE *stream, long offset, int whence);

描述:The fseek() function sets the file position indicator for the stream pointed to by stream. The new position, measured in bytes, is obtained by adding offset bytes to the position specified by whence. If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the start of the file, the current position indicator, or end-of-file(in this case,offset may be smaller than zero), respectively. A successful call to the fseek() function clears the end-of-file indicator for the stream and undoes any effects of the ungetc(3) function on the same stream.

成功:返回0
失败:返回-1

 

7.int fclose(FILE *fp);

描述:The fclose() function flushes the stream pointed to by fp (writing any buffered output data using fflush(3)) and closes the underlying file

descriptor(fp).

成功:返回0

失败:返回EOF

posted on 2013-10-20 16:59  运动和行动  阅读(317)  评论(0编辑  收藏  举报