read函数原型:
ssize_t read(int fd, void *buf, size_t count);参数: fd:open函数的返回值buf:缓冲区,存储要读取的数据count:缓冲区能存储的最大字节数sizeof(buf) 返回值: -1:失败成功: >0:读出的字节数=0:文件读完了write函数原型:
ssize_t write(int fd, const void *buf, size_t count);参数: fd:open函数的返回值buf:要写到文件中的数据count:strlen(buf) 返回值: -1:失败>0:写入到文件中的字节数 /************************************************************************ > File Name: read_write.c > Author: CurryCoder > Mail: 1217096231@qq.com > Created Time: 2020年07月04日 星期六 21时34分47秒 ************************************************************************/ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include <fcntl.h> int main(int argc,const char* argv[]){ // 打开文件 int fd = open("english.txt",O_RDWR); printf("fd = %d\n",fd); // 打开另一个文件,写操作 int fd1 = open("temp", O_WRONLY | O_CREAT, 0664); printf("fd1 = %d\n", fd1); // read char buf[4096]; int len = read(fd, buf, sizeof(buf)); while(len > 0){ // 数据写入文件中 int ret = write(fd1, buf, len); printf("ret = %d\n", ret); // read len = read(fd, buf, sizeof(buf)); } close(fd); close(fd1); return 0; } lseek函数原型: off_t lseek(int fd, off_t offset, int whence);whence参数: SEEK_SETSEEK_CURSEEK_END 使用: a.文件指针移动到头部; lseek(fd, 0, SEEK_SET); b.获取文件指针当前的位置; int len = lseek(fd, 0, SEEK_CUR); c.获取文件长度; int len = lseek(fd, 0, SEEK_END); 文件的拓展: 文件原大小为100K,现在括扩展为1100K:lseek(fd, 1000, SEEK_END);最后,做一次写操作:write(fd, “a”, 1); /************************************************************************ > File Name: lessk.c > Author: CurryCoder > Mail: 1217096231@qq.com > Created Time: 2020年07月05日 星期日 10时40分22秒 ************************************************************************/ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include <fcntl.h> int main(int argc, const char* argv[]){ int fd = open("english.txt", O_RDWR); if(fd == -1){ perror("open error"); exit(1); } // 文件拓展 int len = lseek(fd, 1000, SEEK_END); write(fd, "a", 1); printf("len=%d\n", len); close(fd); return 0; }chmod()修改文件权限
int chmod(const char *pathname, mode_t mode);参数: pathname:文件名mode:文件权限,八进制数chown()修改文件所有者和所属组
int chown(const char *pathname, uid_t owner, gid_t group);参数: pathname:文件名owner:整型值,用户ID 用户ID的查看:/etc/passwd group:整型值,用户组ID 用户组ID的查看:/etc/group /************************************************************************ > File Name: chmod.c > Author: CurryCoder > Mail: 1217096231@qq.com > Created Time: 2020年07月05日 星期日 15时50分54秒 ************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> int main(int argc, const char* argv[]){ if(argc < 3){ printf("a.out filename mode\n"); exit(1); } int mode = strtol(argv[2], NULL, 8); int ret = chmod(argv[1], mode); if(ret == -1){ perror("chmod"); exit(1); } ret = chown(argv[1], 1001, 1002); if(ret == -1){ perror("chown"); exit(1); } return 0; } truncate()修改文件大小 int truncate(const char *path, off_t length);参数: path:文件名length:文件的最终大小 比原来小,删除后面的部分比原来大,向后扩展(1).打开一个目录opendir()
DIR *opendir(const char *name); 参数:目录名返回值:指向目录的指针(2).读目录
struct dirent { ino_t d_ino; // 此目录进入点的inode ff_t d_off; // 目录文件开头至此目录进入点的位移 signed short ind d_reclen; // d_name的长度,不包含NULL字符 unsigned char d_type; // 重点:d_name所指的文件类型 har d_name[256]; // 重点:文件名 };d_type:
DT_BLK:块设备DT_CHR:字符设备DT_DIR:目录DT_LNK:软链接DT_FIFO:管道DT_REG:普通文件DT_SOCK:套接字DT_UNKNOWN:未知struct dirent* readdir(DIR* dirp);
参数:opendir的返回值返回值:目录项结构体(3).关闭目录
int closedir(DIR *dirp); /************************************************************************ > File Name: getfilenumber.c > Author: CurryCoder > Mail: 1217096231@qq.com > Created Time: 2020年07月05日 星期日 21时24分32秒 ************************************************************************/ #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include <stdlib.h> #include <dirent.h> // 读指定目录中的文件个数 int get_file_num(const char* root){ int total = 0; // 读目录 DIR* dir = NULL; dir = opendir(root); if(dir == NULL){ perror("opendir error"); exit(1); } // 循环读目录中的文件 struct dirent* ptr = NULL; while((ptr = readdir(dir)) != NULL){ // 不处理.和..目录 if(strcmp(".", ptr->d_name) == 0 || strcmp("..", ptr->d_name) == 0){ continue; } // 判断是否是普通文件 if(ptr->d_type == DT_REG){ total++; } // 如果是目录,则需要递归 if(ptr->d_type == DT_DIR){ // 求出子目录 char path[1024] = {0}; sprintf(path, "%s/%s", root, ptr->d_name); total += get_file_num(path); } } // 关闭目录 closedir(dir); return total; } int main(int argc, const char* argv[]){ if(argc < 2){ printf("./a.out path\n"); exit(1); } int total = get_file_num(argv[1]); printf("%s目录下的普通文件共有:%d个\n", argv[1],total); return 0; }复制文件描述符
int dup(int oldfd);参数:oldfd要复制的文件描述符返回值:取最小的且没被占用的文件描述符dup调用成功:有两个文件描述符指向同一个文件int dup2(int oldfd, int newfd); 假设:oldfd指向hello文件,newfd指向world文件 a.假设newfd已经指向了一个文件,首先断开close与那个文件的链接,newfd指向oldfd指向的文件(文件描述符的重定向)b.newfd没有被占用,newfd指向oldfd指向的文件c.oldfd和newfd指向同一个文件,不做任何处理改变已经打开的文件的属性:fcntl
变参函数复制一个已有的文件描述符:int ret = fcntl(fd, F_DUPFD)获取/设置文件状态标志 open的flags参数获取文件状态标识:int flag = fcntl(fd, F_GETFL)设置文件状态标识: flag = flag | O_APPEND;fcntl(fd, F_SETFL, flag) 可更改的几个标识:O_APPEND、O_NONBLOCK(常用) int dup2(int oldfd, int newfd); /************************************************************************ > File Name: dup.c > Author: CurryCoder > Mail: 1217096231@qq.com > Created Time: 2020年07月05日 星期日 22时07分30秒 ************************************************************************/ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> int main(void){ int fd = open("tmp", O_RDWR | O_CREAT, 0664); if(fd == -1){ perror("open"); exit(1); } // 复制文件描述符 int fd2 = dup(fd); // int fd2 = fcntl(fd, F_DUPFD); // 写文件 char* p = "代码改变世界..."; write(fd2, p, strlen(p)); close(fd2); char buf[1024]; lseek(fd, 0, SEEK_SET); read(fd, buf, sizeof(buf)); printf("buf = %s\n", buf); close(fd); return 0; } /************************************************************************ > File Name: dup2.c > Author: CurryCoder > Mail: 1217096231@qq.com > Created Time: 2020年07月05日 星期日 22时07分30秒 ************************************************************************/ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> int main(void){ int fd = open("tmp", O_RDWR | O_CREAT | O_APPEND, 0664); if(fd == -1){ perror("open"); exit(1); } int fd2 = open("tmp1", O_RDWR | O_CREAT | O_APPEND, 0664); if(fd2 == -1){ perror("open open"); exit(1); } // 复制文件描述符 dup2(fd, fd2); // 写文件 char* p = "code change the world..."; write(fd2, p, strlen(p)); close(fd2); char buf[1024]; lseek(fd, 0, SEEK_SET); read(fd, buf, sizeof(buf)); printf("buf = %s\n", buf); close(fd); return 0; } /************************************************************************ > File Name: fcntl.c > Author: CurryCoder > Mail: 1217096231@qq.com > Created Time: 2020年07月05日 星期日 22时28分26秒 ************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main(void) { int fd; int flag; // 测试字符串 char *p = "我们是一个有中国特色的社会主义国家!!!!!!"; char *q = "呵呵, 社会主义好哇。。。。。。"; // 只写的方式打开文件 fd = open("test.txt", O_WRONLY); if(fd == -1) { perror("open"); exit(1); } // 输入新的内容,该部分会覆盖原来旧的内容 if(write(fd, p, strlen(p)) == -1) { perror("write"); exit(1); } // 使用 F_GETFL 命令得到文件状态标志 flag = fcntl(fd, F_GETFL, 0); if(flag == -1) { perror("fcntl"); exit(1); } // 将文件状态标志添加 ”追加写“ 选项 flag |= O_APPEND; // 将文件状态修改为追加写 if(fcntl(fd, F_SETFL, flag) == -1) { perror("fcntl -- append write"); exit(1); } // 再次输入新内容,该内容会追加到旧内容的后面 if(write(fd, q, strlen(q)) == -1) { perror("write again"); exit(1); } // 关闭文件 close(fd); return 0; } /************************************************************************ > File Name: fc.c > Author: CurryCoder > Mail: 1217096231@qq.com > Created Time: 2020年07月05日 星期日 22时30分16秒 ************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main(void) { int fd; int flag; // 测试字符串 char *p = "我们是一个有中国特色的社会主义国家!!!!!!"; char *q = "我无言以对,只能呵呵。。。。。。"; // 只写的方式打开文件 fd = open("test.txt", O_RDONLY); if(fd == -1) { perror("open"); exit(1); } // 使用 F_GETFL 命令得到文件状态标志 flag = fcntl(fd, F_GETFL, 0); if(flag == -1) { perror("fcntl"); exit(1); } flag = O_RDWR; if(fcntl(fd, F_SETFL, flag) == -1) { perror("fcntl -- append write"); exit(1); } // 再次输入新内容,该内容会追加到旧内容的后面 if(write(fd, q, strlen(q)) == -1) { perror("write again"); exit(1); } // 关闭文件 close(fd); return 0; }