Linux文件非读写操作

Linux文件非读写操作

access()

//检查是否调用进程有Access这个文件的权限,如果文件是一个符号链接,会将它解引用,成功返回0,失败返回-1设errno
#include <unistd.h>
int access(const char *pathname, int mode);
/*mode(Bitwise Or) :
F_OK    //文件是否存在
R_OK    //文件是否存在且授予了该进程读权限
W_OK    //文件是否存在且授予了该进程写权限
X_OK    //文件是否存在且授予了该进程执行权限
*/
if(0==access("./a.txt",F_OK))
    printf("file exists\n");
if(0==access("./a.txt",R_OK))
    printf("file exists and grants read permission\n");

fstat()、stat()、lstat()

//读取文件状态,fstat()从fd读取,stat () i从pathname读取,lstat()从pathname读取,如果文件是符号链接,则返回符号链接本身的信息,而其他的函数会对链接解引用
#include<unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
int fstat(int fd, struct stat *buf);
int stat (const char *pathname,     struct stat *buf);
int lstat(const char *pathname,     struct stat *buf);
/*
struct stat {
    dev_t   st_dev;         /* ID of device containing file */
    ino_t   st_ino;         /* inode number */
    mode_t  st_mode;        /* protection */                    //八进制usigned int o%
    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 */          //ld%
    blksize_t   st_blksize; /* blocksize for filesystem I/O */
    blkcnt_t    st_blocks;  /* number of 512B blocks allocated */
    time_t  st_atime;       /* time of last access */   
    time_t  st_mtime;       /* time of last modification */     //ld%,秒
    time_t  st_ctime;       /* time of last status change */
};

一些POSIX宏可以用来检查文件类型
S_ISREG(m)      //is it a regular file?     //if yes, return 1
S_ISDIR(m)      //is it directory?
S_ISCHR(m)      //is it character device?
S_ISBLK(m)      //is it block device?
S_ISFIFO(m)     //is it FIFO (named pipe)?
S_ISLNK(m)      //is it symbolic link?      (Not in POSIX.1-1996.)
S_ISSOCK(m)     //is it socket?             (Not in POSIX.1-1996.)
*/

获取文件大小

  1. fseek()把offset移到SEEK_END, 再用ftell()返回文件的大小
  2. lseek() , 返回文件的大小
    3.stat(), struct stat st; st.st_size的数值就是文件大小
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
    struct stat st={};
    int res=stat("./a.txt",&st);
    if(-1==res)
        printf("stat"),exit(-1);
    printf("st_mode=%o,st_size=%ld,st_mtime=%ld\n",st.st_mode,st.st_size,st.st_mtime);
    if(S_ISREG(st.st_mode))
        printf("Regular file\n");
    if(S_ISDIR(st.st_mode))
        printf("Dir file\n");
    printf("file prot:%o\n",st.st_mode&0777);
    printf("file size:%ld\n",st.st_size);
    printf("latest modify:%s",ctime(&st.st_mtime));
    struct tm* pt=localtime(&st.st_mtime);
    printf("latest modify:%d-%d-%d-%02d:%02d:%02d\n",
        1900+pt->tm_year,1+pt->tm_mon,pt->tm_mday,pt->tm_hour,pt->tm_min,pt->tm_sec);
                                //%02输出2个字符宽度,不足两位输出0站位
    return 0;
}

chmod()、fchmod():

//更改文件的权限,这两个函数的唯一区别就是指定文件的方式不一样,成功返回0,失败返回-1,设errno
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);

truncate()、ftruncate():

//截断文件为指定大小,成功返回0,失败返回-1设errno
#include <unistd.h>
#include <sys/types.h>
int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
/*
如果原大小  >  指定大小,多余的文件数据被丢弃
如果原大小e  <  指定大小,文件被扩展,扩展的部分被填充为'\0'
*/
/*-----------------------
 * chmod(),truncate();
 * -------------------*/
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
int res=chmod("a.txt",0600);
if(-1==res)
    perror("chmod"),exit(-1);

int res=truncate("a.txt",100);
if(-1==res)
    perror("truncate"),exit(-1);
/*------------------------------------------------------
ftruncate();mmap();结构体指针初始化
----------------------------------------------------*/
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/mman.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
    int id;
    char name[20];
    double salary;
}Emp;
int main(){
    int fd=open("./emp.dat",O_RDWR|O_TRUNC|O_CREAT,0664);
    if(-1==fd)
        perror("open"),exit(-1);
    printf("open success\n");
    int res=ftruncate(fd,3*sizeof(Emp));        //要用sizeof,且是Emp(类型)不是emp(对象)
    if(-1==res)
        perror("ftruncate"),exit(-1);
    
    void* pv=mmap(NULL,3*sizeof(Emp),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    if((void*)-1==pv)
        perror("mmap"),exit(-1);

    Emp *pe=pv;
    pe[0].id=1001;strcpy(pe[0].name,"zhangfei");pe[0].salary=3000;
    pe[1].id=1002;strcpy(pe[1].name,"guanyu");pe[1].salary=3500;
    pe[2].id=1003;strcpy(pe[2].name,"liubei");pe[2].salary=4000;
//  *(pe+2)={1003,"liubei",4000};
//  pe+2->salary=4000;              //ATTENTION:    ->优先级比+高, 所以一定要加()
    printf("map success\n");
    res=munmap(pv,3*sizeof(Emp));
    if(-1==res)
        perror("munmap"),exit(-1);
    printf("unmap success\n");
    res=close(fd);
    if(-1==res)
        perror("close"),exit(-1);
    printf("close success\n");
    return 0;
}

猜你喜欢

转载自www.linuxidc.com/Linux/2016-10/135762.htm
今日推荐