//這是一個(gè)使用lseek在一個(gè)文件中連續(xù)寫(xiě)入字符串的應(yīng)用
#include
#include
#include
int main(int argc,char *argv[])
{
int temp,seektemp,i,j;
int fd; //文件描述符
char writebuf[17] = "this is a test!\n"; //字符串最后加上回車換行
if(argc!= 2) //如果參數(shù)錯(cuò)誤
{
printf("Plz input the corrcet file name as './exam309lseekFun filename string'!\n");
return 1; //如果參數(shù)不正確則退出
}
fd = open(*(argv+1),O_RDWR|O_CREAT,S_IRWXU); //打開(kāi)文件如果沒(méi)有則創(chuàng)建
temp = write(fd,writebuf,sizeof(writebuf)); //寫(xiě)入數(shù)據(jù)
seektemp = lseek(fd,0,SEEK_CUR); //獲得當(dāng)前的偏移量
for(i=0;i<10;i++) //連續(xù)寫(xiě)入10個(gè)字符串
{
j = sizeof(writebuf) * (i+1); //計(jì)算下一次的偏移量
seektemp = lseek(fd,j,SEEK_SET);
temp = write(fd,writebuf,strlen(writebuf)); //寫(xiě)入數(shù)據(jù)
}
close(fd); //關(guān)閉文件
return 0;
}
當(dāng)前題目:[Linux文件]使用lseek函數(shù)的偏移量來(lái)分次寫(xiě)入數(shù)據(jù)的實(shí)例
文章出自:
http://weahome.cn/article/gdoipj.html