0

I wrote down a small C code on linux with a creat function. I used it couple of times with the same file name and the same mode and every time it's overwrites my file with a new time and permission with no EEXIST error.

 if (creat(name, mode) < 0)
{
    printf("something went wrong with create! %s\n", strerror(errno));
    exit(1);
}

What is the problem?

Dvir Naim
  • 335
  • 1
  • 3
  • 12
  • If that's not what you want, why are you calling `creat`?! – David Schwartz May 31 '16 at 21:28
  • I want to create but I want it to check first if this file exists. – Dvir Naim May 31 '16 at 21:30
  • Can you be very, very specific about exactly what you want to do? What do you want to happen if the file already exists? What do you want to happen if the file does not exist? What do you want to happen if the file exists but is a symbolic link? – David Schwartz May 31 '16 at 21:35

2 Answers2

4

EEXIST is only returned if O_CREAT | O_EXCL is used in the flags to open. While creat(2) does imply O_CREAT, it does not imply O_EXCL, only O_CREAT | O_WRONLY | O_TRUNC.

You should instead use open.

Jeremy Roman
  • 16,137
  • 1
  • 43
  • 44
  • so every time I want to create a file I need to try to open it first? there is no way to check while creating if the file exists to avoid overwrite it? – Dvir Naim May 31 '16 at 21:29
  • 2
    @DvirNaim You want to open a file right, creating it if it doesn't not exist and failing if it does exist? Is that correct? If so, you don't need to try to open it "first", all you need to do is try to open it in such a way that the open fails if the file already exists. – David Schwartz May 31 '16 at 21:38
  • found it..I tought that creat will do the job for me..I was worng...http://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c-cross-platform – Dvir Naim May 31 '16 at 21:41
  • But `creat()` opens the file too! – Jean-Baptiste Yunès May 31 '16 at 22:16
3

The creat() function is the same as:

open(path, O_CREAT | O_TRUNC | O_WRONLY, mode);

You need the flags O_APPEND to write at the end of the file

So you should use open() read() write()

EDIT

exemple :

#include <fcntl.h>
#include <unistd.h>

int is_file_exist (char *filename)
{
  struct stat   buffer;   
  return (stat (filename, &buffer) == 0);
}

void open_read_write() {
  int fd;

  if (!is_file_exist("./file"))
    return ;
  // open a file descriptor, if not, create
  fd = open("./file", O_RDWR | O_APPEND);
  // thanks to O_APPEND, write() writes at the end of the file
  write(fd, "hello world\n", 12);
  // close the file descriptor
  close(fd); // important !
}
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
albttx
  • 3,444
  • 4
  • 23
  • 42
  • so every time I want to create a file I need to try to open it first? there is no way to check while creating if the file exists to avoid overwrite it? – Dvir Naim May 31 '16 at 21:29
  • Exactly, but you can create your own function to make the task easily the `open()` function give you a `file descriptor`, it's needed for reading or writing into a file – albttx May 31 '16 at 21:30
  • How..? which tools do I need..? – Dvir Naim May 31 '16 at 21:32
  • found it...http://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c-cross-platform – Dvir Naim May 31 '16 at 21:40