测试1:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #define SIZE 1024 int main() { // 打开要读的文件 int fd = open("big", O_RDWR|O_CREAT, 0777); if (fd == -1) { perror ("open fd1"); return -1; } printf ("%d\n", fd); // 设置这个文件的偏移指针到 1G处 lseek (fd, 20, SEEK_SET); printf ("等待2写数据\n"); getchar(); char *buf = "hello"; write (fd, "a", 1); getchar(); close (fd); return 0; }
测试2:
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #define SIZE 1024 int main() { // 打开要读的文件 int fd1 = open("abc", O_RDWR|O_CREAT, 0777); if (fd1 == -1) { perror ("open fd1"); return -1; } printf ("abc fd = %d\n", fd1); int fd = open("big", O_RDWR|O_CREAT, 0777); if (fd == -1) { perror ("open fd1"); return -1; } printf ("bif fd = %d\n", fd); // 设置这个文件的偏移指针到 1G处 lseek (fd, 10, SEEK_SET); char *buf = "12345"; write (fd, buf, strlen(buf)); getchar(); close (fd); return 0; }