四、Linux系统编程-文件和IO(二)文件的读写
来源:互联网 发布:数据分析师招聘信息 编辑:程序博客网 时间:2024/05/09 23:19
一、read、write
函数声明:
#include <unistd.h>ssize_t read(int fd, void *buf, size_t count);ssize_t write(int fd, const void *buf, size_t count);
一旦有了与一个打开文件描述相连的文件描述符,只要该文件是用O_RDONLY或O_RDWR标志打开的,就可以用read()系统调用从该文件中读取字节
(1)、函数原型:
ssize_t read(intfd, void *buf,size_t count);
参数:
- fd:想要读的文件的文件描述符
- buf: 指向内存块的指针,从文件中读取来的字节放到这个内存块中
- count : 从该文件复制到buf中的字节个数
返回值
- 如果出现错误,返回-1
- 读文件结束,返回0
- 否则返回从该文件复制到规定的缓冲区中的字节数
(2)、用write()系统调用将数据写到一个文件中
函数原型:
ssize_t write(intfd, const void *buf,size_t count);
函数参数:
- fd:要写入的文件的文件描述符
- buf:指向内存块的指针,从这个内存块中读取数据写入 到文件中
- count:要写入文件的字节个数
返回值
- 如果出现错误,返回-1
- 如果写入成功,则返回写入到文件中的字节个数
注意:read返回值如果大于0表示读到的字节数。而write返回值大于0表示数据已经从用户缓冲区拷贝到内核缓冲区,并不一定已经写入到文件中。如果想要阻塞到写入到文件之后返回。可以在open中flag中指定 O_SYNC或者调用fsync将内核数据写入文件。
下面是一个copy程序的示例:
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <unistd.h>#define ERR_EXIT(m) \ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ }while(0)int main(int argc,char* argv[]){ int infd; int outfd; if (argc != 3) { fprintf(stderr,"Usage %s src dest",argv[0]); } infd = open(argv[1],O_RDONLY); if (infd == -1) ERR_EXIT("open src error"); if ((outfd = open(argv[2],O_WRONLY | O_CREAT | O_TRUNC,0644)) == -1) ERR_EXIT("open dest error"); char buf[1024]; int nread; while((nread = read(infd,buf,sizeof(buf))) > 0) { write(outfd,buf,nread); } close(infd); close(outfd); return 0;}
二、lseek
在Linux系统中,随机访问就变得很简单,你所需做的只是将当前文件移值改变到有关的位置,它将迫使一次read()或write()发生在这一位置。(除非文件被O_APPEND打开,在这种情况下,任何write调用仍将发生在文件结束处)。
(1)、函数声明:
#include <sys/types.h>#include <unistd.h>off_t lseek(int fd, off_t offset, int whence);(2)、函数参数:
- fd:文件描述符
- offset:偏移量
- whence:偏移起始位置
SEEK_SET
The offset is set to offset bytes.
SEEK_CUR
The offset is set to its current location plus offset bytes.
SEEK_END
The offset is set to the size of the file plus offset bytes.
The offset is set to offset bytes.
SEEK_CUR
The offset is set to its current location plus offset bytes.
SEEK_END
The offset is set to the size of the file plus offset bytes.
(3)、返回值:
新的文件偏移值
示例:
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <unistd.h>#define ERR_EXIT(m) \ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ }while(0)int main(){ int fd; fd = open("aa.txt",O_RDWR | O_CREAT,0655); if (fd == -1) ERR_EXIT("open error"); char buf[1024]; int ret = read(fd,buf,4); if (ret == -1) ERR_EXIT("read error"); ret = lseek(fd,3,SEEK_CUR); if (ret == -1) ERR_EXIT("lseek error"); printf("current offset is %d\n",ret); ret = write(fd,"GGGG",4);//如果原位置有数据会直接覆盖 if (ret == -1) ERR_EXIT("write error"); close(fd); return 0;}
三、目录访问(opendir、readdir、closedir)
功能:打开一个目录
函数原型:
#include <sys/types.h>#include <dirent.h>DIR *opendir(const char *name);DIR *fdopendir(int fd);参数:目录名或者打开目录的描述符
返回值:打开成功,返回一个目录指针,打开失败,则返回0
示例:
#include <string.h>#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <unistd.h>#include <dirent.h>#define ERR_EXIT(m) \ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ }while(0)int main(int argc,char* argv[]){ DIR *dir = opendir("."); struct dirent *de; while((de = readdir(dir)) != NULL) { printf("%s\n",de->d_name); } closedir(dir); printf("\n\n"); printf("fdopendir...\n"); struct dirent *dp; int fd; fd = open(".",O_RDONLY); if (fd == -1) ERR_EXIT("open error"); DIR *ndir = fdopendir(fd); while((dp = readdir(ndir)) != NULL) { printf("%s\n",dp->d_name); } closedir(ndir);//这里会把打开目录的文件描述符关闭,所以不能显示关闭 return 0;}
四、rmdir、mkdir
功能:创建和删除目录
(1)、函数原型:
#include <unistd.h>int rmdir(const char *pathname);
#include <sys/stat.h>#include <sys/types.h>int mkdir(const char *pathname, mode_t mode);
(2)、文件名,打开模式
(3)、返回值:成功返回0,失败返回-1注意:rmdir只能删除空目录
示例:
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <unistd.h>#define ERR_EXIT(m) \ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ }while(0)int main(){ int ret; if ((ret = mkdir("testdir",0644)) == -1) ERR_EXIT("mkdir error"); system("ls"); printf("\n\n"); if ((ret = rmdir("testdir")) == -1) ERR_EXIT("rmdir error"); system("ls"); return 0;}
五、chmod、fchmod
功能:通过文件路径和文件描述符改变文件权限。
函数声明:
#include <sys/stat.h>int chmod(const char *path, mode_t mode);int fchmod(int fd, mode_t mode);函数参数:路径或者描述符,新的权限值
返回值:成功返回0,失败返回-1
六、chown、fchown、lchown
功能:改变文件所有者
函数声明:
#include <unistd.h>int chown(const char *path, uid_t owner, gid_t group);int fchown(int fd, uid_t owner, gid_t group);int lchown(const char *path, uid_t owner, gid_t group);
函数参数:文件路径或者描述符,新的所有者id,新的组id。若是link文件,1改变link指向文件,3改变link文件自身。
返回值:成功返回0,失败返回-1
0 0
- 四、Linux系统编程-文件和IO(二)文件的读写
- 六、Linux系统编程-文件和IO(四)文件共享、复制文件描述符
- Linux系统编程文件IO
- Linux文件IO(文件的读写)
- 三、Linux系统编程-文件和IO(一)文件的打开和关闭
- Linux系统编程--文件(四)
- 五、Linux系统编程-文件和IO(三)文件元数据的获取
- 文件IO编程四
- Linux系统编程--文件(二)
- 10---IO编程---文件读写
- 文件IO编程二
- linux系统编程之文件io小结
- 【Linux系统编程】文件IO操作
- Linux系统编程-文件IO函数
- Linux系统编程-文件IO(一)
- linux系统编程-文件IO操作
- Linux系统文件读写
- 【Linux环境编程入门】四、文件操作的系统调用
- 算法学习(3)分治策略(棋盘覆盖)
- HDU 4553 线段树双关键字区间合并
- Oj2-重复数字中寻找破坏重复次数的那个
- 虚拟机下进行串口调试硬件
- 查看私有方法
- 四、Linux系统编程-文件和IO(二)文件的读写
- 矩阵的逆
- quick-cocos2d-x 学习系列之九filter
- 线性回归
- hdu 3790 最短路径问题
- WampServer修改MySQL密码
- Android之安全机制
- vim 心得
- Android SDK Manager国内无法更新的解决方案