Linux中append函数的用法,linux C代码 open函数参数:O_APPEND问题求助
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
沒有截圖,直接上代碼吧。
原代碼是這樣的:
#include
#include
#include
#include
#include
int main()
{
int fd=-1;
ssize_t size=-1;
off_t offset=-1;
char buf1[]="0124553";
char buf2[]="dsfafads";
char filename[]="test1.txt";
int len=8;
fd=open(filename,O_RDWR|O_CREAT,S_IRWXU);
if(-1==fd)
{
printf("fd open fd:%d",fd);
return -1;
}
size=write(fd,buf1,len);
if(size!=len)
{
printf("size buf1 write size:%d len=%d fd=%d" ,size,len,fd);
return -1;
}
offset = lseek(fd,32,SEEK_SET);
if(-1==offset)
{return -1;}
size=write(fd,buf2,len);
if(size!=len)
{
return -1;
}
close(fd);
return 0;
}
運行結果是正常的,在test1.txt文件中輸入了兩段字符,中間隔著'\0'字符。
我把 fd=open(filename,O_RDWR|O_CREAT,S_IRWXU);中的參數O_RDWR改為O_APPEND參數,這個參數書上的定義為打開文件的時候,每次寫操作前,將寫操作的位置移動到文件的結尾處。然后我在test1.txt中預先加入一段字符,再次運行修改參數后編譯的程序,經測試在size=write(fd,buf1,len);這個地方size獲得的值是-1。后來又直接用簡單的write和open函數進行O_APPEND參數測試,還是出現同樣的問題,這是為什么?求大佬講解一下。
總結
以上是生活随笔為你收集整理的Linux中append函数的用法,linux C代码 open函数参数:O_APPEND问题求助的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 酷炫的SVG 动态图标
- 下一篇: WEB 服务器配置