• Linux中文件编程


    Linux中文件编程可以使用两种方法:
        1、Linux系统调用(依赖于Linux系统)
        2、C语言库函数(与系统独立,在任何系统下,使用C语言库函数操作文件的方法都是相同的)

    int creat(const char *pathname, mode_t mode)
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    pathname:要创建的文件名(包含路径,缺省为当前路径)
    mode:创建模式
    常见的创建模式:
        S_IRUSR    可读
        S_IWUSR    可写
        S_IXUSR    可执行
        S_IRWXU    可读、写、执行
    除了可以使用上述宏以外,还可以直接使用数字来表示文件的访问权限:
    可执行:1
    可写:2
    可读:4
    文件的权限即是上述值的和,无任何权限:0

    creat函数返回文件描述符
    文件描述符:
    文件描述符是非负整数。打开现存文件或新建文件时,内核会返回一个文件描述符。使用文件描述符来指定待读写的文件。描述符范围:0-OPEN_MAX,现在很多系统中OPEN_MAX=1024。
    标准输入(standard input)的文件描述符是0
    标准输出(standard output)是1
    标准错误(standard error)是2。启动操作系统的时候这三个文件描述符是打开的。
    系统级的I/O操作函数都是针对文件描述符的.即打开文件时返回一个文件描述符,然后可以直接对该文件描述符进行操作.
    比如:open、read、write

    int open(const char *pathname, int flags)
    int open(const char *pathname, int flags, mode_t mode)
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>

    int close(int fd)
    #include <unistd.h>

    ssize_t read(int fd, const void *buf, size_t count)
    ssize_t write(int fd, const void *buf, size_t count)
    #include <unistd.h>

    off_t lseek(int fildes, off_t offset, int whence)
    #include <sys/types.h>
    #include <unistd.h>
    whence可使用下述值:
    SEEK_SET:相对文件开头
    SEEK_CUR:相对文件读写指针的当前位置
    SEEK_END:相对文件末尾
    offset可取负值,表示向前移动
    例:(功能利用lseek来计算文件长度)
        lseek(fd, 0, SEEK_END)

    int access(const char *pathname, int mode)

    struct dirent *readdir(DIR *dir)
    #include <sys/types.h>
    #include <dirent.h>

    int unlink(const char *pathname)
    #include <unistd.h>

    ...


  • 相关阅读:
    java 流重定向
    Linux下用鼠标滚轮
    AutoPager的简单实现
    App Store Review Guidelines ——苹果大慈大悲啊
    利用CSS3特性巧妙实现漂亮的DIV箭头
    Have you read it before?
    Windows Phone 7上的GPS应用编程详解。
    说一下Path类
    远程截取屏幕内容
    争论VB.NET与C#哪个好?——智商低下的举动
  • 原文地址:https://www.cnblogs.com/qinkai/p/2429722.html
Copyright © 2020-2023  润新知