读文件程序实例1 #include <fcnt1.h> main() { int fd; char lilbuf 20],bigbuf[1024]; fd=open(“/etc/passwd”,O_RDONLY); read(fd,lilbuf,20); read(fd,bigbuf,1024); read(fd,lilbuf,20); } 11
11 读文件程序实例1 #include <fcntl.h> main( ) { int fd; char lilbuf[20], bigbuf[1024]; fd = open(“/etc/passwd”, O_RDONLY); read(fd, lilbuf, 20); read(fd, bigbuf, 1024); read(fd, lilbuf, 20); }
#include <fcntl.h> main()快进程A*/ { int fd; char buf[512]; fd open("/etc/passwd",O_RDONLY); read(fd,buf,sizeof(buf)); /read1 * read(fd,buf,sizeof(buf)); /read2 * } main()进程B*/ { int fd,I; char buf[512]; for(i=0;i<sizeof(buf);i++) bufj=‘a'; fd open("/etc/passwd",O_WRONLY); write(fd,buf,sizeof(buf)); /write1 * write(fd,buf,sizeof(buf)); /write2 * 12
12 #include <fcntl.h> main( ) /* 进程 A */ { int fd; char buf[512]; fd = open(“/etc/passwd”, O_RDONLY); read(fd, buf, sizeof(buf)); /* read1 */ read(fd, buf, sizeof(buf)); /* read2 */ } main( ) /* 进程 B */ { int fd, I; char buf[512]; for(i=0; i<sizeof(buf); i++) buf[i] = ‘a’; fd = open(“/etc/passwd”, O_WRONLY); write(fd, buf, sizeof(buf)); /* write1 */ write(fd, buf, sizeof(buf)); /* write2 */ }
3、文件l/O位置调整Iseek position Iseek (fd,offset,reference); 其中:fd一文件描述符 offset一字节偏移量 reference一偏移参照点:O:从文件头开始 1:从当前位置开始 2:从文件尾开始 下图为Iseek应用实例: 13
13 3、文件I/O位置调整 lseek position = lseek (fd, offset, reference); 其中:fd —— 文件描述符 offset —— 字节偏移量 reference —— 偏移参照点:0:从文件头开始 1:从当前位置开始 2:从文件尾开始 下图为lseek应用实例: