GB | BIG5
|
| 首頁 > 編程技術 > C/C++ > 正文 |
 |
| 文件操作 |
| 本文出自: http://go3.163.com/~axiom999/ (2001-08-22 16:42:36) |
基本上所有的系統調用成功時返回0或正數,失敗時返回負值。
文件的創建與刪除
create系統調用的格式
#include <fcntl.h>
int creat(path,mode)
const char *path; /*文件路徑名*/
int mode; /*文件存取權*/
參數與功能說明:
該調用創建名為path的文件。如果path已存在,則創建文件的進程對文目錄應有執行權限, 並且對該文件有寫權限。執行成功時,原先path文件的內容被清除,其長度被設置為0,原先文件的 mode,uid,gid被保留,而新指定的mode不起作用。成功時返回文件描述符。
remove系統調用的格式
#include <stdio.h>
int remove(path)
const char *path; /*文件路徑名*/
unlink系統調用的格式
#include <stdio.h>
int unlink(path)
const char *path; /*文件路徑名*/
參數與功能說明:
一個文件可以有若幹個路徑名,unlink的作用是刪除某個文件的一個名為path 的路徑名,並將文件i接點的連接計數減一。當計數為0時,若仍有進程在打開文件,文件不能立即被刪除。
文件的打開與關閉
open系統調用的格式
#include <fcntl.h>
int open(path,oflag[,mode])
const char *path; /*文件路徑名*/
int oflag,mode; /*打開方式和存取標志*/
參數與功能說明:
按給定的oflag打開path所指定的文件,可選參數mode一般在創建新文件時使用,其意義與creat調用中的mode一樣。Oflag可取值為
O_RDONLY,O_WRONLY,O_RDWR,O_NDEALY,O_CREAT,O_TRUNC,O_EXCL,O_APPEND,O_SYNC,成功時返回文件描述符。
close系統調用的格式
#include <unistd.h>
int close(fd)
int fd; /*文件描述符*/
參數與功能說明:
釋放指定的文件文件描述符fd,fd意義是用creat.open,dup,fcntl.pipe調用得到的一個文件描述符。
文件的讀寫操作
read系統調用的格式
#include <unistd.h>
int read(fd,buf,nbytes)
int fd; /*文件描述符*/
char *buf; /*緩沖區地址*/
unsigned nbytes /*要讀的字節數*/
參數與功能說明:
執行成功返回讀入的字節數。
write系統調用的格式
#include < unistd.h>
int write(fd,buf,nbytes)
int fd; /*文件描述符*/
char *buf; /*緩沖區地址*/
unsigned nbytes /*要讀的字節數*/
參數與功能說明:
執行成功返回寫入的字節數。
文件的隨機存取
lseek系統調用的格式
#include < unistd.h>
long lseek(fd,offset,inter)
int fd; /*文件描述符*/
int offset; /*文件偏移量*/
int inter; /*對文件偏移量的解釋*/
參數與功能說明:
調整文件的讀寫指針,調整方式有inter給出。Inter可選用的值為SEEK_SET,SEEK_CUT,SEEK_END
文件控制
fcntl系統調用的格式
#include < fcntl.h>
int fcntl (fd,offset,inter)
int fd; /*文件描述符*/
int cmd; /*命令*/
int arg; /*參數*/
參數與功能說明:
對打開的文件進行控制。Arg根據不同的cmd取不同的值。可取為F_DUPFD,F_GETFD,F_GETFL,F_SETFL;;F_GETLK,F_SETLK,F_SETLKW。
flock結構如下:
struct flock
{
short l_type;
short l_whence;
long l_start;
long l_len;
short l_pid;
}
(http://www.fanqiang.com)
進入【UNIX論壇】
|
|
| 相關文章 |
|
|
|
|
 |
★ 樊強制作 歡迎分享 ★ |