| lseek 和 write //验证lseek和write的关系 //无论以何种方式打开文件,lseek都可以在文件中seek到任意的位置 //write写的方式和open函数的方式有直接的关系 //如果open函数打开的时候不用O_APPEND参数,先seek到任意位置,可以在文件中的某个位置 //然后write,则冲调文件里之前的内容
//如果open函数打开的时候用O_APPEND参数,无论seek到任意位置,write都是从文件的末尾开始写的 #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> char buf[] = "$$$$$$$$$$"; int main( void ) { int fd; if( ( fd = open("file",O_RDWR|O_APPEND ) ) < 0 ) { printf("Open Error!\n"); exit(1); } if( lseek( fd, 10, SEEK_SET ) ==-1 ) { printf( "Seek Error!\n" ); exit(1); } write(fd,buf,10); exit(0); }
|