? ? ? 一、文件IO
1、文件io通過(guò)系統(tǒng)調(diào)用來(lái)操作文件
系統(tǒng)調(diào)用:系統(tǒng)提供給用戶(hù)的一組API(接口函數(shù))
??????? open/read/write/close/lseek...
用戶(hù)空間進(jìn)程訪(fǎng)問(wèn)內(nèi)核的接口
把用戶(hù)從底層的硬件編程中解放出來(lái)
極大的提高了系統(tǒng)的安全性
使用戶(hù)程序具有可移植性(同一系統(tǒng)下)
是操作系統(tǒng)的一部分
文件io沒(méi)有緩存區(qū)概念
文件io操作文件的方式:通過(guò)文件描述符
shell默認(rèn)打開(kāi)了三個(gè)文件:
標(biāo)準(zhǔn)輸入? 標(biāo)準(zhǔn)輸出 標(biāo)準(zhǔn)錯(cuò)誤
stdin stdout? stderr????????????????? 用流來(lái)表示
? ????0? ?????? 1?????? 2?????????????????? 用文件描述符表示
2、文件IO相關(guān)函數(shù)
?????? 1、open函數(shù)
?????? 功能:
????????????? 打開(kāi)一個(gè)文件
?????? 頭文件
?????? ?? #include <sys/types.h>
?????? #include <sys/stat.h>
?????? #include <fcntl.h>
?????? 函數(shù)原型
????????????? int open(const char *pathname, int flags);
?????? ?int open(const char *pathname, int flags, mode_t mode);
?????? 參數(shù)
??????? pathname:文件的路徑
????????????? flags:文件打開(kāi)的方式
???????????????????? O_RDONLY:只讀
???????????????????? O_WRONLY:只寫(xiě)
???????????????????? O_RDWR:可讀可寫(xiě)
???????????????????? 這三個(gè)宏互斥(三選一)
???????????????????? O_CREAT:如果文件不存在,則創(chuàng)建文件,此時(shí)用到open函數(shù)的第二種形式。
???????????????????? O_TRUNC:如果文件存在則清空文件的內(nèi)容。
???????????????????? O_APPEND:以追加的方式打開(kāi)文件。
????????????? ??????
?????? mode:文件的權(quán)限,八進(jìn)制(0777)
?????? 返回值
????????????? 成功返回一個(gè)新的文件描述符,失敗返回(-1)并設(shè)置錯(cuò)誤號(hào)。
?????? 2、close函數(shù)
?????? 作用:
????????????? 關(guān)閉文件
?????? 頭文件
?????? ?? #include <unistd.h>
?????? 函數(shù)原型
????????????? int close(int fd);
?????? 參數(shù)
??????? 想要關(guān)閉的文件描述符
?????? 返回值
????????????? 成功返回0,失敗返回(-1)并設(shè)置錯(cuò)誤號(hào)。
?????? 3、read/write
????????1、read
????????????? 作用:
???????????????????? 向文件讀取內(nèi)容
?????? ??? 頭文件:
??????????? #include <unistd.h>
????????????? 函數(shù)原型
?????? ???????? ssize_t read(int fd, void *buf, size_t count);
????????????? 參數(shù)
???????????????????? fd:想要操作的文件描述符
???????????????????? buf:讀取到的內(nèi)容放到哪個(gè)地址
???????????????????? count:讀取到的文件中的字符數(shù)量
????????????? 返回值:
????????????? 成功返回已經(jīng)讀取到的字節(jié)數(shù),失敗返回-1并設(shè)置錯(cuò)誤號(hào),讀取到文件末尾返回0。
????????2、write
????????????? 作用:
???????????????????? 向文件寫(xiě)入內(nèi)容
?????? ??? 頭文件:
?? ?????????#include <unistd.h>
????????????? 函數(shù)原型
? ????????????? ssize_t write(int fd, void *buf, size_t count);
????????????? 參數(shù)
????????????? ?????? fd:想要寫(xiě)入的文件描述符
???????????????????? buf:需要寫(xiě)入文件的內(nèi)容
???????????????????? count:寫(xiě)入文件中字符數(shù)量
????????????? 返回值:
???????????????????? 成功返回已經(jīng)寫(xiě)入的字節(jié)數(shù),失敗返回-1并設(shè)置錯(cuò)誤號(hào)。
?????? 4、lseek
????????????? 作用:
???????????????????? 偏移文件內(nèi)的書(shū)簽
?????? ??? 頭文件:
??????????? #include <sys/types.h>
????????????? 函數(shù)原型
?????? ???????? off_t lseek(int fd, off_t offset, int whence);
????????????? 參數(shù)
???????????????????? fd:想要寫(xiě)入的文件描述符
???????????????????? offset:偏移量,正數(shù)表示后偏移,負(fù)數(shù)前偏移,0不偏移
???????????????????? whence:書(shū)簽偏移的起始位置
?????????????????????????????????? SEEK_SET:文件頭
?????????????????????????????????? SEEK_CUR:當(dāng)前位置
?????????????????????????????????? SEEK_END:文件末尾
????????????? 返回值:
???????????????????? 成功返回當(dāng)前偏移到的位置,失敗返回-1并設(shè)置錯(cuò)誤號(hào)。
? ? ? 二、文件和目錄
1、stat
?????? #include <sys/types.h>
?????? #include <sys/stat.h>
?????? #include <unistd.h>
int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);
stat,lstat通過(guò)傳入的pathname獲取文件屬性
fstat通過(guò)用open打開(kāi)的文件的文件描述符獲取文件屬性
stat在操作軟鏈接文件時(shí)會(huì)獲取源文件的相關(guān)屬性
lstat在操作軟鏈接文件時(shí)會(huì)獲取鏈接文件的相關(guān)屬性
成功返回0,失敗返回-1,并設(shè)置錯(cuò)誤號(hào)
相關(guān)結(jié)構(gòu)體
struct stat {
?????????????? dev_t???? st_dev;???????? /* ID of device containing file */
?????????????? ino_t???? st_ino;???????? /* Inode number */
?????????????? mode_t??? st_mode;??????? /* File type and mode */
?????????????? nlink_t?? st_nlink;?????? /* Number of hard links */
?????????????? uid_t???? st_uid;???????? /* User ID of owner */
?????????????? gid_t???? st_gid;???????? /* Group ID of owner */
?????????????? dev_t???? st_rdev;??????? /* Device ID (if special file) */
?????????????? off_t???? st_size;??????? /* Total size, in bytes */
?????????????? blksize_t st_blksize;???? /* Block size for filesystem I/O */
?????????????? blkcnt_t? st_blocks;????? /* Number of 512B blocks allocated */
?????????????????????????????????? /* Since Linux 2.6, the kernel supports nanosecond
????????????????? precision for the following timestamp fields.
????????????????? For the details before Linux 2.6, see NOTES. */
?????????????? struct timespec st_atim;? /* Time of last access */
?????????????? struct timespec st_mtim;? /* Time of last modification */
?????????????? struct timespec st_ctim;? /* Time of last status change */
?????????? #define st_atime st_atim.tv_sec????? /* Backward compatibility */
?????????? #define st_mtime st_mtim.tv_sec
?????????? #define st_ctime st_ctim.tv_sec
?????????? };
1、文件類(lèi)型(可以用以下的宏確定文件類(lèi)型,這些宏的參數(shù)都是struct stat結(jié)構(gòu)中的ts_mode成員)
S_ISREG(m)? is it a regular file?
S_ISDIR(m)? directory?
S_ISCHR(m)? character device?
S_ISBLK(m)? block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m)? symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
2、文件權(quán)限
?????? 存放在st_mode低9位,從高位到低位分別表示用戶(hù)權(quán)限、同組用戶(hù)權(quán)限、其它用戶(hù)權(quán)限,該位為1表示有權(quán)限,0表示沒(méi)有權(quán)限
3、鏈接數(shù)
直接通過(guò)st_nlink得到
4、文件所有者
getpwuid函數(shù)相關(guān)操作得到
5、文件所屬組
getgrgid函數(shù)相關(guān)操作得到
6、文件字節(jié)大小
直接通過(guò)st_size得到
7、文件更新時(shí)間
ctime(&st_ctime)得到文件的更新時(shí)間
#include <stdio.h>
#include <sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<string.h>
#include<pwd.h>
#include<grp.h>
#include<time.h>
int main(int argc, char *argv[])
{
? if(2!=argc)
? ? {
? ? ? ? printf("error\n");
? ? }
? ? struct stat st;
? ? lstat(argv[1],&st);
? ? if(S_ISREG(st.st_mode))
? ? ? ? printf("-");
? ? else if (S_ISDIR(st.st_mode))
? ? ? ? printf("d");
? ? else if(S_ISLNK(st.st_mode))
? ? ? ? printf("l");
? ? int i=8;
? ? for ( ; i >= 0; i-=3)
? ? {
? ? ? ? if(st.st_mode&1<<i!=0)
? ? ? ? ? printf("r");
? ? ? ? else
? ? ? ? ? ? printf("-");
? ? ? ? if(st.st_mode&1<<(i-1)!=0)
? ? ? ? ? ? printf("w");
? ? ? ? else
? ? ? ? ? ? printf("-");
? ? ? ? if(st.st_mode&1<<(i-2)!=0)
? ? ? ? ? ? printf("x");
? ? ? ? else
? ? ? ? ? ? printf("-");
? ? }
//用戶(hù)名
? ? struct passwd *pw=getpwuid(st.st_uid);
//鏈接數(shù)? ?
printf(" %ld",st.st_nlink);
? ? printf(" %s ",pw->pw_name);
struct group *gr =getgrgid(st.st_gid);
//用戶(hù)組名? ?
printf( "%s",gr->gr_name);
//長(zhǎng)度? ?
printf(" %ld",st.st_size);
? ? char arr[100]={0};
//獲取時(shí)間
? ? strcpy(arr,ctime(&st.st_mtime));
? ? if(arr[strlen(arr)-1]=='\n')
? ? arr[strlen(arr)-1]='\0';
? ? printf(" %s ",arr);
? ? puts("");
? ? return 0;
}
2、目錄文件函數(shù)
??? 1、opendir
?????? 作用:
?????????? 打開(kāi)一個(gè)目錄文件
?????? 頭文件
?????????? #include<sys/types.h>
?????????? #include<dirent.h>
?????? 函數(shù)原型:
?????????? DIR *opendir(const char *name)
?????? 參數(shù):
?????????? name:目錄文件
?????? 返回值:
?????????? 成功返回目錄流指針,失敗返回 NULL并設(shè)置錯(cuò)誤號(hào)。
??? 2、readdir
??? 作用:
?????? 讀取目錄當(dāng)中一個(gè)文件的屬性
??? 頭文件:
?????? dirent.h
??? 函數(shù)文件
?????? struct dirent *readdir(DIR * dirp);
??? 參數(shù):
?????? dirp:想要操作的目錄流指針
??? 返回值:
?????? 成功返回讀取到的文件的相關(guān)信息的地址,被保存在一個(gè)struct dirent結(jié)構(gòu)體中,失敗或者讀完了所有文件的返回NULL。
相關(guān)結(jié)構(gòu)體:
struct dirent {
?? ? ? ? ? ? ? ino_t ? ? ? ?? d_ino; ? ? ? /* Inode number */
?? ? ? ? ? ? ? off_t ? ? ? ?? d_off; ? ? ? /* Not an offset; see below */
?? ? ? ? ? ? ? unsigned short d_reclen; ?? /* Length of this record */
?? ? ? ? ? ? ? unsigned char? d_type; ? ?? /* Type of file; not supported
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? by all filesystem types */
?? ? ? ? ? ? ? char ? ? ? ? ? d_name[256]; /* Null-terminated filename */
?? ? ? ? ? };
?
?????? 3、closedir
?????? 作用:
?????? ?????? 關(guān)閉目錄流指針
?????? 頭文件:
?????? ?????? #include <sys/types.h>
??? ????????????? #include <dirent.h>
?????? 函數(shù)原型:
?????? ?????? int closedir(DIR *dirp);
?????? 參數(shù):
?????? ?????? dirp:目錄流指針
?????? 返回值:
?????? 成功返回0,失敗返回-1并設(shè)置錯(cuò)誤號(hào)
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-671196.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-671196.html
到了這里,關(guān)于IO線(xiàn)程,文件IO(open),文件(stat)與目錄(opendir)屬性的讀取的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!