完成如下问题的猜想,并设计相应的程序验证自己的猜想:
当设定O_APPEND标志后,能否使用lseek函数修改当前文件偏移量?若能修改,读写操作发生在文件何处?
当设定O_APPEND标志后,可以否使用lseek函数修改当前文件偏移量,在使用lseek函数后文件偏移发生了改变。
读操作发生在修改后的文件偏移处
写操作发生在
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main (void)
{
int fd =open ("test.txt", 0_RDWR : 0_APPEND);
if(fd < 0){
cout << "open error" <<endl;
}
//打开文件
//获取文件fd
char readBuf;
readBuf = (char*) malloc (sizeof (char) *3) ;
//设置read空间
int pos =lseek (fd, B, SEEK_SET);
cout <<"pos_1: " < <pos < <endl;
//使用lseek修改问文件偏移
read (fd,readBuf, 3) ;
cout <<readBuf < <endl;
//获得read结果
write (fd, "test_0",strlen ("test_0") ) ;
//获得write结果
pos =lseek(fd, 1, SEEK_SET);
cout<<"pos_2: "<<pos <<endl;
write (fd, "test_1",strlen ("test_1") ) ;
close (fd) ;
return 0;
}