1)普通文件(regular file).這是最常見(jiàn)的文件類(lèi)型,這種文件包含了某種形式的數據.
6)套接口(socket).用于進(jìn)程間的網(wǎng)絡(luò )通信。套接口也可用于在一臺宿主機上的進(jìn)程之間的非網(wǎng)絡(luò )通信。
umask函數為進(jìn)程設置文件方式創(chuàng )建屏蔽字,并返回以前的值。
#include <sys/types.h>
#include <sys/stat.h>
mode_t umask(mode_t cmask);
返回:以前的文件方式創(chuàng )建屏蔽字
chmod和fchmod函數:改變存取許可權
#include <sys/types.h>
#include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int filedes, mode_t mode);
兩個(gè)函數返回:若成功則為0,若出錯則為-1
chown、fchown和lchown函數:改變所有者和組所有者
#include <sys/types.h>
#include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);
int fchown(int filedes, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);
三個(gè)函數返回:若成功則為0,若出錯則為-1
截短文件可以調用函數truncate和ftruncate
#include <sys/types.h>
#include <unistd.h>
int truncate(const char *pathname, off_t length);
int ftruncate(int filedes, off_t length);
兩個(gè)函數返回;若成功則為0,若出錯則為-1
rename:文件重命名
#include <stdio.h>
int rename(const char *oldname, const char *newname);
返回:若成功則為0,若出錯則為-1
utime:修改文件時(shí)間
#include <sys/types.h>
#include <utime.h>
int utime(const char *pathname, const struct utimbuf *times);
返回:若成功則為0,若出錯則為-1
struct utimbuf {
time_t actime; /*access time*/
time_t modtime; /*modification time*/
}
三、目錄操作
mkdir和rmdir:創(chuàng )建和刪除目錄
#include <sys/types.h>
#include <sys/stat.h>
int mkdir(const char *pathname, mode_t mode);
返回:若成功則為0,若出錯則為-1
函數rmdir刪除一個(gè)空目錄。
#include <unistd.h>
int rmdir(const char *pathname);
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *pathname);
返回:若成功則為指針,若出錯則為NULL
struct dirent *readdir(DIR *dir);
返回:若成功則為指針,若在目錄尾或出錯則為NULL
void rewinddir(DIR *dir);
int closedir(DIR *dir);
返回:若成功則為0,若出錯則為-1
定義在頭文件<dirent.h>中的dirent結構如下所示:
struct dirent
{
__ino_t d_ino;
__off_t d_off;
unsigned short int d_reclen;
unsigned char d_type;
char d_name[256];
};
chdir、fchdir和getcwd函數:改變目錄和取得當前目錄
#include <unistd.h>
int chdir(const char *pathname);
int fchdir(int filedes);
char *getcwd(char *buf, size_t size);
兩個(gè)函數的返回:若成功則為0,若出錯則為-1
四、文件I/O接口概述
文件有5個(gè)操作:打開(kāi)、關(guān)閉、讀、寫(xiě)、定位。有兩套接口都可以用來(lái)完成這些操作,一類(lèi)是系統調用接口,對應的函數是open
、close、read、write、lseek,另一類(lèi)是標準I/O庫接口,對應的函數是fopen、fclose、fread、fwrite、fseek。
調用open函數可以打開(kāi)或創(chuàng )建一個(gè)文件。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int oflag,.../*, mode_t mode */);
返回:若成功為文件描述符,若出錯為-1
用creat函數創(chuàng )建一個(gè)新文件。
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat(const char * pathname, mode_t mode) ;
返回:若成功為只寫(xiě)打開(kāi)的文件描述符,若出錯為-1
此函數等效于:
open(pathname, O_WRONLY|O_CREAT|O_TRUNC, mode);
close函數關(guān)閉一個(gè)打開(kāi)文件:
#include <unistd.h>
int close(int filedes);
返回:若成功為0,若出錯為-1
read函數從打開(kāi)文件中讀數據。
#include <unistd.h>
ssize_t read(int filedes, void *buff, size_t nbyt s);
返回:讀到的字節數,若已到文件尾為0,若出錯為-1
write函數向打開(kāi)文件寫(xiě)數據。
#include <unistd.h>
ssize_t write(int filedes, const void * buff, size_t nbytes);
返回:若成功為已寫(xiě)的字節數,若出錯為-1
用lseek顯式地定位一個(gè)打開(kāi)文件。
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int filedes, off_t offset, int whence);
返回:若成功為新的文件位移,若出錯為-1?
若whence是SEEK_SET,則將該文件的位移量設置為距文件開(kāi)始處offset個(gè)字節。
若whence是SEEK_CUR,則將該文件的位移量設置為其當前值加offset,offset可為正或負。
若whence是SEEK_END,則將該文件的位移量設置為文件長(cháng)度加offset,offset可為正或負。