赞
踩
/*
*@pram: fd :文件描述符
* offset :偏移量
* whence :偏移开始的位置 (SEEK_SET / SEEK_CUR / SEEK_END)
*@return value :offset到文件末尾的字节数
*/
off_t lseek(int fd, off_t offset, int whence);
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int cal_len(const char *pathname) { int fd = -1; // fd 就是file descriptor,文件描述符 int ret = -1; // 第一步:打开文件 fd = open(pathname, O_RDONLY); if (-1 == fd) // 有时候也写成: (fd < 0) { //printf("\n"); perror("文件打开错误"); // return -1; return -1; } //else //{ //printf("文件打开成功,fd = %d.\n", fd); //} // 此时文件指针指向文件开头 // 我们用lseek将文件指针移动到末尾,然后返回值就是文件指针距离文件开头的偏移量,也就是文件的长度了 ret = lseek(fd, 0, SEEK_END); return ret; } int main(int argc, char *argv[]) { int fd = -1; // fd 就是file descriptor,文件描述符 int ret = -1; if (argc != 2) { printf("usage: %s filename\n", argv[0]); _exit(-1); } printf("文件长度是:%d字节\n", cal_len(argv[1])); return 0; }
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]) { int fd = -1; // fd 就是file descriptor,文件描述符 char buf[100] = {0}; char writebuf[20] = "abcdefg"; int ret = -1; // 第一步:打开文件 fd = open("222.txt", O_RDWR | O_CREAT, 0666); //fd = open("a.txt", O_RDONLY); if (-1 == fd) // 有时候也写成: (fd < 0) { //printf("\n"); perror("文件打开错误"); // return -1; _exit(-1); } else { printf("文件打开成功,fd = %d.\n", fd); } ret = lseek(fd, 10, SEEK_SET); printf("lseek, ret = %d.\n", ret); #if 1 // 第二步:读写文件 // 写文件 ret = write(fd, writebuf, strlen(writebuf)); if (ret < 0) { //printf("write失败.\n"); perror("write失败"); _exit(-1); } else { printf("write成功,写入了%d个字符\n", ret); } #endif #if 1 // 读文件 ret = read(fd, buf, 20); if (ret < 0) { printf("read失败\n"); _exit(-1); } else { printf("实际读取了%d字节.\n", ret); printf("文件内容是:[%s].\n", buf); } #endif // 第三步:关闭文件 close(fd); _exit(0); }
int dup(int oldfd);
/*DESCRIPTION
The dup() system call creates a copy of the file descriptor oldfd, using the lowest-numbered unused file descriptor for the new descriptor.
After a successful return, the old and new file descriptors may be used interchangeably. They refer to the same open file description (see open(2)) and thus share file offset and
file status flags; for example, if the file offset is modified by using lseek(2) on one of the file descriptors, the offset is also changed for the other.
The two file descriptors do not share file descriptor flags (the close-on-exec flag). The close-on-exec flag (FD_CLOEXEC; see fcntl(2)) for the duplicate descriptor is off.
*/
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #define FILENAME "1.txt" int main(void) { int fd1 = -1, fd2 = -1; fd1 = open(FILENAME, O_RDWR | O_CREAT | O_TRUNC, 0644); if (fd1 < 0) { perror("open"); return -1; } printf("fd1 = %d.\n", fd1); close(1); // 1就是标准输出stdout // 复制文件描述符 fd2 = dup(fd1); // fd2一定等于1,因为前面刚刚关闭了1,这句话就把 // 1.txt文件和标准输出就绑定起来了,所以以后输出到标准输出的信息就 // 可以到1.txt中查看了。 printf("fd2 = %d.\n", fd2); printf("this is for test.\n"); close(fd1); return -1; }
int dup2(int oldfd, int newfd);
/*
The dup2() system call performs the same task as dup(), but instead of using the lowest-numbered unused file descriptor, it uses the file descriptor number specified in newfd. If the file
descriptor newfd was previously open, it is silently closed before being reused.
The steps of closing and reusing the file descriptor newfd are performed atomically. This is important, because trying to implement equivalent functionality using close(2) and dup() would be subject to race conditions, whereby newfd might be reused between the two steps. Such reuse could happen because the main program is interrupted by a signal handler that allocates a file descriptor, or because a parallel thread allocates a file descriptor.
Note the following points:
* If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed.
* If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does nothing, and returns newfd.
*/
1.dup2和dup的作用是一样的,都是复制一个新的文件描述符。但是dup2允许用户指定新的文件描述符的数字。
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #define FILENAME "1.txt" int main(void) { int fd1 = -1, fd2 = -1; fd1 = open(FILENAME, O_RDWR | O_CREAT | O_TRUNC, 0644); if (fd1 < 0) { perror("open"); return -1; } printf("fd1 = %d.\n", fd1); //close(1); // 1就是标准输出stdout // 复制文件描述符 //fd2 = dup(fd1); // fd2一定等于1,因为前面刚刚关闭了1,这句话就把 // 1.txt文件和标准输出就绑定起来了,所以以后输出到标准输出的信息就 // 可以到1.txt中查看了。 fd2 = dup2(fd1, 16); printf("fd2 = %d.\n", fd2); // printf("this is for test"); while (1) { write(fd1, "aa", 2); sleep(1); write(fd2, "bb", 2); } close(fd1); return -1; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。