open

头文件:#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>

定义函数:

    int open(const char * pathname, int flags);int open(const char * pathname, int flags, mode_t mode);

函数说明:

参数 pathname 指向欲打开的文件路径字符串. 下列是参数flags 所能使用的旗标:

O_RDONLY 以只读方式打开文件
O_WRONLY 以只写方式打开文件
O_RDWR 以可读写方式打开文件. 上述三种旗标是互斥的, 也就是不可同时使用, 但可与下列的旗标利用OR(|)运算符组合.
O_CREAT 若欲打开的文件不存在则自动建立该文件.
O_EXCL 如果O_CREAT 也被设置, 此指令会去检查文件是否存在. 文件若不存在则建立该文件, 否则将导致打开文件错误. 此外, 若O_CREAT 与O_EXCL 同时设置, 并且欲打开的文件为符号连接, 则会打开文件失败.
O_NOCTTY 如果欲打开的文件为终端机设备时, 则不会将该终端机当成进程控制终端机.
O_TRUNC 若文件存在并且以可写的方式打开时, 此旗标会令文件长度清为0, 而原来存于该文件的资料也会消失.
O_APPEND 当读写文件时会从文件尾开始移动, 也就是所写入的数据会以附加的方式加入到文件后面.
O_NONBLOCK 以不可阻断的方式打开文件, 也就是无论有无数据读取或等待, 都会立即返回进程之中.
O_NDELAY 同O_NONBLOCK.
O_SYNC 以同步的方式打开文件.
O_NOFOLLOW 如果参数pathname 所指的文件为一符号连接, 则会令打开文件失败.
O_DIRECTORY 如果参数pathname 所指的文件并非为一目录, 则会令打开文件失败。 注:此为Linux2. 2 以后特有的旗标, 以避免一些系统安全问题.

参数mode 则有下列数种组合, 只有在建立新文件时才会生效, 此外真正建文件时的权限会受到umask 值所影响, 因此该文件权限应该为 (mode-umaks).

S_IRWXU00700 权限, 代表该文件所有者具有可读、可写及可执行的权限.
S_IRUSR 或S_IREAD, 00400 权限, 代表该文件所有者具有可读取的权限.
S_IWUSR 或S_IWRITE, 00200 权限, 代表该文件所有者具有可写入的权限.
S_IXUSR 或S_IEXEC, 00100 权限, 代表该文件所有者具有可执行的权限.
S_IRWXG 00070 权限, 代表该文件用户组具有可读、可写及可执行的权限.
S_IRGRP 00040 权限, 代表该文件用户组具有可读的权限.
S_IWGRP 00020 权限, 代表该文件用户组具有可写入的权限.
S_IXGRP 00010 权限, 代表该文件用户组具有可执行的权限.
S_IRWXO 00007 权限, 代表其他用户具有可读、可写及可执行的权限.
S_IROTH 00004 权限, 代表其他用户具有可读的权限
S_IWOTH 00002 权限, 代表其他用户具有可写入的权限.
S_IXOTH 00001 权限, 代表其他用户具有可执行的权限.

返回值:若所有欲核查的权限都通过了检查则返回0 值, 表示成功, 只要有一个权限被禁止则返回-1.

错误代码:

EEXIST 参数pathname 所指的文件已存在, 却使用了O_CREAT 和O_EXCL 旗标.
EACCESS 参数pathname 所指的文件不符合所要求测试的权限.
EROFS 欲测试写入权限的文件存在于只读文件系统内.
EFAULT 参数pathname 指针超出可存取内存空间.
EINVAL 参数mode 不正确.
ENAMETOOLONG 参数 pathname 太长.
ENOTDIR 参数pathname 不是目录.
ENOMEM 核心内存不足.
ELOOP 参数pathname 有过多符号连接问题.
EIO I/O 存取错误.
(1)       pread()形式:#include<unistd.h>ssize_t  pread (int filedes,   void *buf,  size_t  nbytes,  off_t  offset );成功:返回读到的字节数;出错:返回-1;到文件结尾:返回0原因:由于lseek和read 调用之间,内核可能会临时挂起进程,所以对同步问题造成了问题,调用pread相当于顺序调用了lseek 和 read,这两个操作相当于一个捆绑的原子操作。实现:文件(由filedes所指)-读nbytes字节->内存buf中。补充:调用pread时,无法中断其定位和读操作,另外不更新文件指针。(2)       pwrite()形式:#include<unistd.h>ssize_t  pwrite (int filedes,   const void *buf,  size_t  nbytes,  off_t  offset );成功:返回已写的字节数;出错:返回-1;原因:由于lseek和write 调用之间,内核可能会临时挂起进程,所以对同步问题造成了问题,调用pwrite相当于顺序调用了lseek 和 write,这两个操作相当于一个捆绑的原子操作。实现:文件(由filedes所指)<-写nbytes字节-内存buf中。补充:调用pwrite时,无法中断其定位和读操作,另外不更新文件指针。

附加说明:使用 access()作用户认证方面的判断要特别小心, 例如在access()后再作open()空文件可能会造成系统安全上的问题.

范例

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
main()
{int fd, size;char s[] = "Linux Programmer!\n", buffer[80];fd = open("/tmp/temp", O_WRONLY|O_CREAT);write(fd, s, sizeof(s));close(fd);fd = open("/tmp/temp", O_RDONLY);size = read(fd, buffer, sizeof(buffer));close(fd);printf("%s", buffer);
}

执行

Linux Programmer!

acess

access函数功能描述:检查调用进程是否可以对指定的文件执行某种操作。用法:
#include<unistd.h>
#include<fcntl.h>
int access(constchar*path…)
access函数
功能描述: [喝小酒的网摘]http://blog.const.net.cn/a/17126.htm
检查调用进程是否可以对指定的文件执行某种操作。

用法:
#include <unistd.h>
#include <fcntl.h>

int access(const char *pathname, int mode);

参数:
pathname: 需要测试的文件路径名。
mode: 需要测试的操作模式,可能值是一个或多个R_OK(可读?), W_OK(可写?), X_OK(可执行?) 或 F_OK(文件存在?)组合体。

返回说明:
成功执行时,返回0。失败返回-1,errno被设为以下的某个值
EINVAL: 模式值无效
EACCES: 文件或路径名中包含的目录不可访问
ELOOP : 解释路径名过程中存在太多的符号连接
ENAMETOOLONG:路径名太长
ENOENT: 路径名中的目录不存在或是无效的符号连接
ENOTDIR: 路径名中当作目录的组件并非目录
EROFS: 文件系统只读
EFAULT: 路径名指向可访问的空间外
EIO: 输入输出错误
ENOMEM: 不能获取足够的内核内存
ETXTBSY:对程序写入出错

linux 判断文件是否存在的access()示例:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>  int main()
{  if((access("test.c",F_OK))!=-1)  {  printf("文件 test.c 存在.\n ");  }  else  {  printf("test.c 不存在!\n ");  }  if(access("test.c",R_OK)!=-1)  {  printf("test.c 有可读权限\n ");  }  else  {  printf("test.c 不可读. \n");  }  if(access("test.c",W_OK)!=-1)  {  printf("test.c 有可写权限\n ");  }  else  {  printf("test.c 不可写.\n ");  }  if(access("test.c",X_OK)!=-1)  {  printf("test.c 有可执行权限\n ");  }  else  {  printf("test.c 不可执行.\n ");  }  return 0;
}

ftruncate

首先 man ftruncate 看下帮助手册NAMEtruncate, ftruncate - truncate a file to a specified lengthSYNOPSISint truncate(const char *path, off_t length);int ftruncate(int fd, off_t length);DESCRIPTIONThe truncate() and ftruncate() functions cause the regular file named by path or referenced by fd to be truncated to a size of precisely length bytes.If the file previously was larger than this size, the extra data is lost.  If the file previously was shorter, it is extended, and the extended part reads as null bytes ('\0').The file offset is not changed.If  the  size  changed,  then the st_ctime and st_mtime fields (respectively, time of last status change and time of last modification; see stat(2)) for the file are updated, and the set-user-ID andset-group-ID permission bits may be cleared.With ftruncate(), the file must be open for writing; with truncate(), the file must be writable.

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main(void)
{int fd;const char *s1 = "0123456789";const char *s2 = "abcde";fd = open("test.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);/* if error */write(fd, s1, strlen(s1));ftruncate(fd, 0);// lseek(fd, 0, SEEK_SET);write(fd, s2, strlen(s2));close(fd);return 0;
}



在使用ftruncate后,在重新写入需要使用lseek重新定位

lstat

 #include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysmacros.h>int main(int argc, char *argv[])
{struct stat sb;if (argc != 2) {fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);exit(EXIT_FAILURE);}if (lstat(argv[1], &sb) == -1) {perror("lstat");exit(EXIT_FAILURE);}printf("ID of containing device:  [%lx,%lx]\n",(long) major(sb.st_dev), (long) minor(sb.st_dev));printf("File type: ");switch (sb.st_mode & S_IFMT) {case S_IFBLK:  printf("block device\n");            break;case S_IFCHR:  printf("character device\n");        break;case S_IFDIR:  printf("directory\n");               break;case S_IFIFO:  printf("FIFO/pipe\n");               break;case S_IFLNK:  printf("symlink\n");                 break;case S_IFREG:  printf("regular file\n");            break;case S_IFSOCK: printf("socket\n");                  break;default:       printf("unknown?\n");                break;}printf("I-node number:            %ld\n", (long) sb.st_ino);printf("Mode:                     %lo (octal)\n",(unsigned long) sb.st_mode);printf("Link count:               %ld\n", (long) sb.st_nlink);printf("Ownership:                UID=%ld   GID=%ld\n",(long) sb.st_uid, (long) sb.st_gid);printf("Preferred I/O block size: %ld bytes\n",(long) sb.st_blksize);printf("File size:                %lld bytes\n",(long long) sb.st_size);printf("Blocks allocated:         %lld\n",(long long) sb.st_blocks);printf("Last status change:       %s", ctime(&sb.st_ctime));printf("Last file access:         %s", ctime(&sb.st_atime));printf("Last file modification:   %s", ctime(&sb.st_mtime));exit(EXIT_SUCCESS);
}

参考资料
1.https://blog.csdn.net/wuxinyanzi/article/details/13620741
2.https://blog.csdn.net/a_ran/article/details/43562429

linux -- open /acess/ftruncate/lstat 函数相关推荐

  1. 【Linux系统】stat、lstat函数

    Linux系统-stat.lstat函数 #include <sys/types.h> #include <sys/stat.h> #include <unistd.h& ...

  2. Linux文件属性2——使用stat函数获取文件属性

    以下内容源于朱有鹏<物联网大讲堂>课程的学习整理,如有侵权,请告知删除. 一.获取文件属性 1.stat.fstat.lstat函数简介 每个文件都附带有一些属性: 属性信息存在于文件本身 ...

  3. Linux系统编程1——系统函数

    文章目录 前言 一.C标准函数与系统函数的区别 1.什么是系统调用 2.文件描述符 3.相关函数 3.1 open函数 3.2 close函数 3.3 read函数 3.4 write函数 3.5 l ...

  4. Linux下的内存对齐函数

    在Linux下内存对齐的函数包括posix_memalign, aligned_alloc, memalign, valloc, pvalloc,其各个函数的声明如下: int posix_memal ...

  5. UNIX--stat、fstat和lstat函数

    2019独角兽企业重金招聘Python工程师标准>>> stat.fstat和lstat函数(UNIX) #include #include int stat(const char ...

  6. Linux Shell中的延时函数

    Linux Shell中的延时函数 在 linux shell 脚本中经常需要做一些延时处理. 所以经常要用到 sleep 或 usleep 函数. 下面来说一下  sleep 和 usleep 的区 ...

  7. linux kbhit扫描键盘,(转)检测按键(Linux中kbhit()函数的实现)

    http://hi.baidu.com/jtntiivtemcnsue/item/90689ce172ee912c5a7cfb1b 编写过MS-DOS程序的人通常都会查找Linux下等同于kbhit的 ...

  8. 文件和目录之stat、fstat和lstat函数

    #include <sys/stat.h> int stat( const char *restrict pathname, struct stat *restrict buf ); in ...

  9. linux启动程序api编程,Linux编程中关于API函数与系统调用间关系

    用户态xyz()函数,内核最终一般会调用形如sys_xyz()的服务例程来处理(不过也有一些例外,这里暂时不考虑) 函数xyz()是直接提供给用户编程使用的.图中"SYSCALL" ...

  10. Linux 编程中的API函数和系统调用的关系【转】

    转自:http://blog.chinaunix.net/uid-25968088-id-3426027.html 原文地址:Linux 编程中的API函数和系统调用的关系 作者:up哥小号 API: ...

最新文章

  1. CentOS6怎么样设置ADSL上网
  2. ubuntu 更新软件
  3. 修改服务器的时区为gmt,将GMT转换为服务器本地时区C#
  4. java 循环结构和常用的类 总结笔记
  5. linux相关(find/grep/awk/sed/rpm)
  6. 水平方向的算子检测竖直边缘理解
  7. springmvc的一些优化设置
  8. Gradle方式构建Java多项目
  9. ORACLE start with… connect by prior 子句用法
  10. matlab 矩阵动态,matlab – 如何以块为单位动态重塑矩阵?
  11. php socket 执行,PHP异步调用socket实现代码
  12. vtk 实现mimics软件中的Split/Merge算法
  13. 文件扩展名(后缀名)是什么?win10怎么显示扩展名?
  14. 【AI视野·今日CV 计算机视觉论文速览 第192期】Thu, 6 May 2021
  15. 线性代数01:函数对向量、矩阵的梯度(向量、矩阵求导)
  16. 数据可视化平台Superset 简介
  17. 高分系列(GF1-GF7)卫星介绍
  18. 树莓派语音控制开关灯
  19. python 全栈开发,Day106(结算中心(详细),立即支付)
  20. 零基础学Arcgis(六)|空间数据采集与管理(3)数据编辑

热门文章

  1. Go 语言为Fibonacci函数实现Read方法
  2. python之路--day?--初始面向对象
  3. 为何 Emoji 能给产品设计(营销)带来如此大的数据增长?
  4. 怎么写出一个Activity
  5. 如何获得getElementById的length这个数值?
  6. Visual Studio 单元测试之六---UI界面测试
  7. [导入]关于中文文本的截取
  8. IRF之BFD-MAD检测
  9. 重建二叉树(C++)
  10. LAMP+Varnish缓存详解(一)——Varnish简介