If file is not exist,create it and write a string into it, else write a string into this file.
but the problem is that open() return a bad file descriptor if this file is exist.
code:
Code:
/*
Linux Open() example source code
Author: Yucoat(www.yucoat.com)
Data: 2011-10-31
Version: v0.1
ChangeLog: Create This program 2011-20-31
*/
#include <fcntl.h> //for open
#include <errno.h> //for errno
#include <stdio.h>
int main()
{
int fd;
if ((fd = open( "./Name", //File name
O_WRONLY|O_EXCL|O_APPEND|O_CREAT, //Mod
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH)) < 0) //Access
{
perror("open");
return -1;
}
if (write(fd, "Yucoat\n", 7) != 7)
perror("Write");
close (fd);
return 0;
}
Thx for every body!I'm very sorry for my bad english!