zoukankan      html  css  js  c++  java
  • Linux 文件管理(系统函数)

    //read函数
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    /*
     STDIN_FILENO:标准输入,值是0
     STDOUT_FILENO:标准输出,值是1
     STDERR_FILENO:标准错误,值是2
     头文件是 unistd.h
    
    read(文件标识符,缓冲区,缓冲区大小)函数是从某个文件中读取缓冲区大小的数据
    ssize_t read(int fd, void *buf, size_t count);
    read()函数的返回值是输入字符的个数+1(+1是因为系统会为输入的字符串加上'') read()相比于scanf()优势在于read()可以读取空格,而scanf()遇到空格结束 但是read不能将数字字符串直接转化成int型,需要调用atoi()函数
    read()函数可以读取键盘上输入的每个字符包括回车换行 atoi()函数的头文件是stdlib.h,功能是将字符串转成数字
    */ int main(int arg,char *args[]) { char buf[50]={0}; int numx=0; int index=read(STDIN_FILENO,buf,sizeof(buf)); printf("返回值是%d,输入字符串是%s ",index,buf); numx=atoi(buf); printf("输入的数字是%d ",numx); return 0; }
    //write函数
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    /*
    ssize_t write(int fd, const void *buf, size_t count);
    write()函数的返回值是写入字符串的大小(不包含'') write()函数向指定文件标识符输出缓冲区数据
    */ int main(int arg,char *args[]) { char buf[50]={0}; strcpy(buf,"fly with you! "); int index=write(STDOUT_FILENO,buf,strlen(buf)); printf("返回值是%d, ",index); return 0; }
    //open函数|close()函数
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    
    /*
     int open(const char *pathname, int flags);
    Opne试图打开参数pathname中的一个文件。
    参数flags指定访问该文件的方式。
    必须把flags设置为O_RDONLY,O_WRONLY,O_RDWR,O_CREAT,O_APPEND分别表示只读、只写、读写、如果文件存在就创建、追加。
    open成功后会返回一个文件描述符。
    open失败后会返回-1,并设置errno变量
    
     errno是int型,表示错误编号,头文件是errno.h
     strerror()函数是打印错误信息,头文件是string.h
     */
    
    int main(int arg,char *args[])
    {
        if(arg<2)
        {
            printf("请输入一个参数!
    ");
            return 0;
        }
        int fd=open(args[1],O_RDONLY);
        if(fd==-1)
        {
            printf("错误信息:%s
    ",strerror(errno));
        }else
        {
            printf("文件标识符是%d
    ",fd);
            char buf[100]={0};
            //读取当前文件的内容
            read(fd,buf,sizeof(buf)-1);
            printf("%s
    ",buf);
            close(fd);
        }
        return 0;
    }
    使用fstat函数获取文件信息。
    int fstat(int fd,struct stat *buf)
    参数fd必须使用open()函数调用返回的有效文件描述符
    使用stat函数获取文件信息
    int stat(const char *path,struct stat *buf)
    参数path是路径加文件名的字符串
    
    结构体stat
    struct stat
    {
        dev_t st_dev;         /*ID of device containing file*/
        ino_t st_ino;         /*inode number*/
        mode_t st_mode;       /*protection*/
        nlink_t st_nlink;     /*numbers 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; /*blocksize for filesystem I/O*/
        blkcnt_t st_blocks;   /*number of blocks allocated*/
        time_t st_atime;      /*time of last access*/
        time_t st_mtime;      /*time of last modification*/
        time_t st_ctime;      /*time of last status change*/
    };
    
    
    为了正确解释文件类型,有一套宏能够计算stat借口的st_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 lin?(Not in POSIX.1-1996)
    S_ISSOCK(m) socket?(Not in POSIX.1-1996)
    //fstat()函数
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    
    int main(int arg,char *args[])
    {
        if(arg<2)
        {
            printf("请输入一个参数!
    ");
            return 0;
        }
        //open file
        /*
           备注:如果是以追加的方式打开文件,必须使用 O_RDWR|O_APPEND;单独的是O_APPEND没有效果!
         */
        int fd=open(args[1],O_RDONLY);
        //Judgment file identifier
        if(fd==-1)
        {
            printf("error msg:%s
    ",strerror(errno));
        }else
        {
            printf("file identifier:%d
    ",fd);
            //define struct stat
            struct stat buf;
            //get file stat
            fstat(fd,&buf);
            //judge file tyep
            if(S_ISREG(buf.st_mode))
            {
                printf("this is regular file!
    ");
            }else if(S_ISDIR(buf.st_mode))
            {
                printf("this id directory!
    ");
            }
            //show file size
            printf("file size:%d
    ",buf.st_size);
            //close file
            close(fd);
        }
        return 0;
    }
    getpass()函数
    读写用户输入,屏幕不回显(一般用于输入密码不显示)
    char * getpass(const char * prompt);
    getpass用于从键盘读取用户输入,但屏幕不回显。
    参数prompt为屏幕提示字符。
    函数返回值为用户键盘输入字符串 
    //getpass()函数的使用
    #include <stdio.h>
    #include <unistd.h>
    
    int main(int arg,char *args[])
    {
        char *s=getpass("input password:");
        printf("your password :%s
    ",s);
        return 0;
    }
  • 相关阅读:
    [Noip2010]乌龟棋
    vijos次小生成树
    hdu3579-Hello Kiki-(扩展欧几里得定理+中国剩余定理)
    hdu1573-X问题-(扩展欧几里得定理+中国剩余定理)
    poj2115-Looooops-(扩展欧几里得定理)
    hdu2669-Romantic-(扩展欧几里得定理)
    poj1061-青蛙的约会-(贝祖定理+扩展欧几里得定理+同余定理)
    hdu1576-A/B-(同余定理+乘法逆元+费马小定理+快速幂)
    hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)
    hdu3189-Just Do It-(埃氏筛+唯一分解定理)
  • 原文地址:https://www.cnblogs.com/zhanggaofeng/p/5791302.html
Copyright © 2011-2022 走看看