Linux——文件1
1、open函数高频使用的Linux系统调用open write read closeLinux自带的工具man手册man 1是普通的shell命令比如lsman 2是系统调用函数比如openwrite说明在Linux系统库的定义int open(const char *pathname, intflags); /* 比较常用*/ int open(const char *pathname, intflags, mode_tmode); //包含的头文件 #include sys/types.h//这里提供类型pid_t和size_t的定义 #include sys/stat.h #include fcntl.h返回值 成功返回句柄我们后面对于文件的读写关闭等都通过句柄来操作。失败返回-1参数说明 grep -nr xxxx./pathname文件的路径名如果只写文件名就默认当前目录如果在文件名加上路径就按照绝对路径来打开文件。flags表示打开文件后用的操作参数说明O_APPEND 表示追加如果原来文件里面有内容则这次写入会写在文件的最末尾。0x00002000O_CREAT 表示如果指定文件不存在则创建这个文件 0x00000100O_EXCL 表示如果要创建的文件已存在则出错同时返回-1并且修改errno 的值。O_TRUNC 表示截断如果文件存在并且以只写、读写方式打开则将其长度截断为0。O_NOCTTY 如果路径名指向终端设备不要把这个设备用作控制终端还是一样的创建hello.cman 2 open打开系统调用官方手册将头文件copy下来按q退出shiftctalv粘贴在hello.c#includestdio.h #include sys/types.h #include sys/stat.h #include fcntl.h int main() { int fd; fd open(/home/pyh/mm,O_RDONLY); if(fd-1) { printf(open failed); return -1 } printf(open successed); return 0; }wq保存并退出进行编译执行因为里面没有mm这个文件多以open failed创建个mm之后再执行其余的命令也是这个用法2、close函数参数说明在Linux系统库的定义int close(int fd);包含的头文件#include unistd.h功能就是简单的关闭文件这个也是和open一样的用法3、linux文件权限1. 权限的三位八进制结构Linux 权限用 3 位八进制数 表示每一位对应一类用户第 1 位用户User 权限第 2 位组用户Group 权限第 3 位其他用户Others 权限每一位的数值由 3 种权限相加得到r读 4w写 2x执行 12. 符号 ↔ 八进制 对照表符号权限八进制含义---0无读、无写、无执行--x1仅执行-w-2仅写-wx3写 执行r--4仅读r-x5读 执行rw-6读 写rwx7读 写 执行常见权限组合权限值符号表示典型用途0644rw-r--r--普通文件所有者可读写其他只读0755rwxr-xr-x可执行程序 / 脚本所有者可读写执行其他只读执行0600rw-------私密文件仅所有者可读写0777rwxrwxrwx开放权限谨慎使用任何人都可读写执行#includestdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include unistd.h //int close(int fd); //int open(const char *pathname, int flags); //int open(const char *pathname, int flags, mode_t mode); int main() { int fd; fd open(file,O_RDWR|O_CREAT,0755); if(fd -1) { printf(open failed\n); perror(why); return -1; } printf(open successed\n); close(fd); return 0; }创建的file文件就是一个可读可写可执行的4、write函数write()会把参数buf所指的内存写入count个字节到参数fd所指的文件内。ssize_twrite (int fd,const void * buf, size_t count);fd: 文件描述符*buf: 写入的数据的首地址count: 写入数据个数返回值如果顺利write()会返回实际写入的字节数len。当有错误发生时则返回-1错误代码存入errno中#includestdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include unistd.h #includestring.h //int close(int fd); //int open(const char *pathname, int flags); //int open(const char *pathname, int flags, mode_t mode); //ssize_t write(int fd, const void *buf, size_t count); int main() { int fd; char writeBuff[128] {0}; char *test hello world!; strcpy(writeBuff,test); fd open(file,O_RDWR|O_CREAT,0755); if(fd -1) { printf(open failed\n); perror(why); return -1; } printf(open successed\n); write(fd,writeBuff[0],12); close(fd); return 0; }编译执行完了之后使用cat来查看文件里的内容5、read函数从打开的fd设备或文件中读取count个字节到buf中ssize_tread(int fd,void * buf, size_tcount);fd: 文件描述符*buf: 读入数据的首地址count: 读入数据的个数返回值成功返回读取的字节数出错返回-1并设置errno如果在调read之前已到达文件末尾则这次read 返回0#includestdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include unistd.h #includestring.h #define fileName file #define writeNum 128 #define readNum 12 //int close(int fd); //int open(const char *pathname, int flags); //int open(const char *pathname, int flags, mode_t mode); //ssize_t write(int fd, const void *buf, size_t count); //ssize_t read(int fd, void *buf, size_t count); int main() { int fd; char writeBuff[writeNum] {0}; char readBuff[readNum] {0}; char *test hello world!; strcpy(writeBuff,test); fd open(fileName,O_RDWR|O_CREAT,0755); if(fd -1) { printf(open failed\n); perror(why); return -1; } printf(open successed\n); //write(fd,writeBuff[0],12); read(fd,readBuff,11); printf(%s\n,readBuff); close(fd); return 0 }其实用法和写法都是于write一样的
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2423399.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!