当前位置: 技术问答>linux和unix
简单的写文件出错...为什么write总是返回 -1的?
来源: 互联网 发布时间:2015-05-18
本文导语: # include # include # include # include # include # include # include # include # include # include # include "ourhdr.h" # include char *filepath="./fortest"; int fd; char buff[]="aa"; void showerr(char *errinfo) { printf("%s",...
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include "ourhdr.h"
# include
char *filepath="./fortest";
int fd;
char buff[]="aa";
void
showerr(char *errinfo)
{
printf("%s",errinfo);
exit(0);
}
int
main(void)
{
if(!(fd=open(filepath,O_CREAT,S_IWUSR)))
showerr("File open ERROR!!");
printf("%d",write (fd,buff,2));
close(fd);
}
# include
# include
# include
# include
# include
# include
# include
# include
# include
# include "ourhdr.h"
# include
char *filepath="./fortest";
int fd;
char buff[]="aa";
void
showerr(char *errinfo)
{
printf("%s",errinfo);
exit(0);
}
int
main(void)
{
if(!(fd=open(filepath,O_CREAT,S_IWUSR)))
showerr("File open ERROR!!");
printf("%d",write (fd,buff,2));
close(fd);
}
|
if(!(fd=open(filepath,O_CREAT,S_IWUSR)))
判断错误,失败时返回的是-1。
另外要用O_EXCL | O_CREAT才能创建文件。
判断错误,失败时返回的是-1。
另外要用O_EXCL | O_CREAT才能创建文件。
|
如果是创建文件指定O_CREAT就行了,指定O_EXCL | O_CREAT时如果要创建的文件已经存在
则返回错误。
你的错误主要是open的返回值判断错了。
则返回错误。
你的错误主要是open的返回值判断错了。