标准C库IO函数

自带帮助文档 第三章


虚拟地址空间

Linux系统IO函数
帮助文档 ~Linux : man 2 open
停留在函数上Shif + k
可进行快速跳转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>
int open(const char *pathname, int flags); 参数: - pathname : 要打开的文件路径 - flags : 对文件的操作权限还有其的他设置 O_RDONLY (只读), O_WRONLY (只写), O_RDWR (可读可写) 这三个是互斥的 返回值:返回一个新的文件描述符,如果失败了返回 -1 ,否则返回一个整形文件描述符 errno:属于Linux系统函数库,库里的一个全局变量,记录的是最近的错误号。 #include <stdio.h> void perror(const char *s); 作用:打印errno对应的错误描述 参数:用户描述,比如hello,最终点输出的内容是 hello:xxx(实际的错误描述)
int open(const char *pathname, int flags, mode_t mode);
|
没有创建 a.txt 代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>
int main() {
int fd = open("a.txt", O_RDONLY);
if(fd == -1) { perror("open"); } close(fd); return 0; }
|
创建文件 touch a.txt
设置umask
umask 022

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>
int main() {
int fd = open("create.txt", O_RDWR | O_CREAT, 0777);
if(fd == -1) { perror("open"); }
close(fd); return 0; }
|

帮助文档 : man 2 copy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>
int main() {
int srcfd = open("english.txt", O_RDONLY); if(srcfd == -1) { perror("opne"); return -1; }
int destfd = open("cpy.txt", O_WRONLY | O_CREAT, 0664); if(destfd == -1) { perror("open"); return -1; }
char buf[1024] = {0};
int len = 0; while((len = read(srcfd, buf, sizeof(buf))) > 0) { write(destfd, buf, len); }
close(destfd); close(srcfd) ; return 0; }
|
帮助文档 : man 2 lseek
Linux 的 lseek 在标准c库中 man 3 fseek
为什么要扩展
比如说下载一个大小文件大小为5G(下载时候也正在用磁盘)假如磁盘大小么有5G(就不能正确下载下来)所以会提起占用一点点在替换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <stdio.h>
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>
int main() { int fd = open("hello.txt", O_RDWR);
if(fd == -1) { perror("open"); return -1; }
int ret = lseek(fd, 100, SEEK_END); if(ret == -1) { perror("open"); return -1; }
write(fd, " ", 1);
close(fd);
return 0; }
|
帮助文档 : man 2 stat


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h>
int main() {
struct stat statbuf;
int ret = stat("a.txt", &statbuf); if(ret == -1) { perror("stat"); return -1; }
printf("size: %ld\n", statbuf.st_size);
return 0; }
|
模拟实现 ls -l 命令

参考文档 man 3 getpwuid
参考文档 man 3 getgrgid
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdlib.h> #include <pwd.h> #include <grp.h> #include <time.h> #include <string.h>
int main(int argc, char* argv[]) {
if(argc < 2) { printf("%s filename\n", argv[0]); return -1; }
struct stat st; int ret = stat(argv[1], &st); if(ret == -1) { perror("stat"); return -1; }
char *perms = (char *)calloc(sizeof(char), 11); switch(st.st_mode & S_IFMT) { case S_IFLNK: perms[0] = 'l'; break; case S_IFDIR: perms[0] ='d'; break; case S_IFREG: perms[0] ='-'; break; case S_IFBLK: perms[0] ='b'; break; case S_IFCHR: perms[0] ='c'; break; case S_IFSOCK: perms[0] ='s'; break; case S_IFIFO: perms[0] ='p'; break; default: perms[0] = '?';
}
perms[1] = (st.st_mode & S_IRUSR) ? 'r' : '-'; perms[2] = (st.st_mode & S_IWUSR) ? 'w' : '-'; perms[3] = (st.st_mode & S_IXUSR) ? 'x' : '-';
perms[4] = (st.st_mode & S_IRGRP) ? 'r' : '-'; perms[5] = (st.st_mode & S_IWGRP) ? 'w' : '-'; perms[6] = (st.st_mode & S_IXGRP) ? 'x' : '-';
perms[7] = (st.st_mode & S_IROTH) ? 'r' : '-'; perms[8] = (st.st_mode & S_IWOTH) ? 'w' : '-'; perms[9] = (st.st_mode & S_IXOTH) ? 'x' : '-';
int linkNum = st.st_nlink;
char *fileUser = getpwuid(st.st_uid)->pw_name; char *fileGrp = getgrgid(st.st_gid)->gr_name; long int fileSize = st.st_size; char *time = ctime(&st.st_mtime);
char mtime[512] ={0}; strncpy(mtime, time, strlen(time) - 1);
char buf[1024]; sprintf(buf, "%s %d %s %s %ld %s %s", perms, linkNum, fileUser, fileGrp, fileSize, mtime, argv[1]); printf("%s\n", buf); return 0; }
|
文件属性
man 2 access
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <stdio.h> #include <sys/stat.h>
int main() { int ret = chmod("a.txt", 0775);
if(ret == -1) { perror("chmod"); return -1; }
return 0; }
|
man 2 truncate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <stdio.h> #include <unistd.h> #include <sys/types.h>
int main() {
int ret = truncate("b.txt", 20); if(ret == -1) { perror("truncat"); return -1; }
return 0; }
|
目录操作函数
man 2 mkdir
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <stdio.h> #include <sys/stat.h> #include <sys/types.h>
int main() { int ret = mkdir("aaa", 0777); if(ret == -1) { perror("mkdir"); return -1; } return 0; }
|
man 2 getcwd
工作路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <stdio.h> #include <unistd.h> #include <string.h>
#include <sys/stat.h> #include <sys/types.h> #include <fcntl.h>
int main() {
char buf[128]; getcwd(buf, sizeof(buf)); printf("当前的工作目录是 : %s\n", buf);
int ret = chdir("../test"); if(ret == -1) { perror("chdir"); return -1; }
int fd = open("w.txt", O_CREAT| O_RDWR, 0664); if(fd == -1) { perror("open"); return -1; }
close(fd);
char buf1[128]; getcwd(buf1, sizeof(buf1)); printf("当前的工作目录是 : %s\n", buf1);
return 0; }
|
man 3 opendir
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <string.h> #include <stdlib.h>
int getFileNum(const char *path);
int main(int argc, char *argv[]) {
if(argc < 2) { printf("%s path\n", argv[0]); return -1; }
int num = getFileNum(argv[1]); printf("普通文件的个数: %d \n", num);
return 0; }
int getFileNum(const char *path) {
DIR *dir = opendir(path);
if(dir == NULL) { perror("opendir"); exit(0); }
struct dirent *ptr;
int total = 0;
while((ptr = readdir(dir)) != NULL) { char *dname = ptr->d_name;
if(strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0) continue;
if(ptr->d_type == DT_DIR) { char newpath[256]; sprintf(newpath, "%s%s", path, dname); total += getFileNum(newpath);
}
if(ptr->d_type == DT_REG) { total++; } } closedir(dir); return total; }
|
dup,dup2函数
man 2 dup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <stdio.h> #include <unistd.h>
#include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h>
int main() { int fd = open("a.txt", O_RDWR | O_CREAT, 0664);
int fd1 = dup(fd);
if(fd1 == -1) { perror("dup"); return -1; }
printf("fd : %d \n fd1 : %d", fd, fd1); close(fd); char *str = "hello, world"; int ret = write(fd1, str, strlen(str));
if(ret == -1) { perror("write"); return -1; } close(fd1); return 0; }
|
man 2 dup2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <string.h>
int main() {
int fd = open("1.txt", O_RDWR | O_CREAT, 0664); if(fd == -1) { perror("open"); }
int fd1 = open("2.txt", O_RDWR | O_CREAT, 0664); if(fd == -1) { perror("open"); return -1; }
printf("fd : %d, fd1 : %d\n", fd, fd1);
int fd2 = dup2(fd, fd1);
if(fd2 == -1) { perror("dup2"); return -1; }
char * str = "hello, dup2"; int len = write(fd1, str, strlen(str)); if(len == -1) { perror("write"); return -1; }
printf("fd : %d, fd1 : %d, fd2 : %d \n", fd, fd1, fd2);
close(fd); close(fd1);
return 0; }
|
man 2 fucnt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <string.h>
int main() {
int fd = open("1.txt", O_RDWR); if(fd == -1) { perror("open"); return -1; }
int flag = fcntl(fd,F_GETFL); if(flag == -1) { perror("fcntl_GET"); return -1; } flag |= O_APPEND;
int ret = fcntl(fd, F_SETFL, flag);
if(ret == -1) { perror("fcntl_SET"); return -1; }
char *str = "nihao"; write(fd, str, strlen(str));
close(fd);
return 0; }
|