[LCP]creating files with open()

Andy Zivkovic andy at zivkotech.net.au
Sat Sep 29 20:17:07 UTC 2001


Hi,

I'm trying to open/create a file if it doesn't exist, then write to it,
however writes are failing. Additionally, the permissions on the file is
really strange. On linux (home) and windows (home) the function I've copied
below creates the file, but the write fails, and the close succeeds. On
Solaris (uni) the open fails.

Is there anything immediately obvious with my program?

The test program I'm using is:

int main(int argc, char *argv[]) {
  int file, ret;

  file = open("file.txt", O_CREAT|O_EXCL);

  printf("file = %d\n", file);

  ret = write(file, "12345\n", 6);

  printf("ret = %d\n", ret);
  if (ret < 0) {
    printf("%s\n", strerror(errno));
  }

  ret = close(file);
  printf("ret = %d\n", ret);
}




More information about the linuxCprogramming mailing list