tags:

views:

470

answers:

2

Hi,

I am trying to read some text from a file and write it to another using open(), read() and write().

This is my open for the file-to-write-to (I want to create a new file and write into it):

fOut = open ("test-1", O_RDWR | O_CREAT | O_SYNC);

This is setting file-permissions to something I don't understand at all. This is the output of ls -l:

---------T 1 chaitanya chaitanya 0 2010-02-11 09:38 test-1

Even the read permission is locked. I tried searching for this, but could not find ANYTHING. Strangely, write() still successfully writes data to the file.

Also, if I do a 'chmod 777 test-1', things start working properly again.

Could someone please let me know where I am going wrong in my open call?

Thanks!

For your reference, I have pasted the complete program below:

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

int main () {

    char buffer[512], ch;

    int fIn, fOut, i;
    ssize_t bytes;
    FILE *fp = NULL;

    //open a file
    fIn = open ("test", O_RDONLY);
    if (fIn == -1) {
        printf("\nfailed to open file.");
        return 1;
    }

    //read from file
    bytes =  read (fIn, buffer, sizeof(buffer));
    //and close it
    close (fIn);

    printf("\nSuccessfully read %d bytes.\n", bytes);

    //Create a new file
    fOut = open ("test-1", O_RDWR | O_CREAT | O_SYNC);

    printf("\nThese are the permissions for test-1\n");
    fflush(stdout);
    system("ls -l test-1");

    //write to it and close it.
    write (fOut, buffer, bytes);
    close (fOut);


    //write is somehow locking even the read permission to the file. Change it.
    system("chmod 777 test-1");

    fp = fopen ("test-1", "r");
    if (fp == NULL) {
        printf("\nCan't open test-1");
        return 1;
    }

    while (1)
    {
        ch = fgetc(fp);
        if (ch == EOF)
            break;
        printf("\n%c", ch);
    }

    fclose (fp);

    return 0;
}
+13  A: 

open() takes a third argument which is the set of permissions, i.e.

open(filename, O_RDWR|O_CREAT, 0666)

0666 is an octal number, i.e. every one of the 6's corresponds to three permission bits

6 = rw

7 = rwx

It's a typical pitfall. The compiler allows you to leave the permission argument away, because when you open an existing file the permission bits don't make sense. But when you forget the argument when you create a file, you get a random set of permissions, e.g. 0000 in your case (---).

antti.huima
For completeness, octal 4 = r
Sam Post
What about hexadecimal 4?
Adam Liss
@Adam Liss: hex 4 = octal 4. Octal is convenient for permissions because each group of 3 bits makes up a unit of permissions and an octal digit. 0644 = 0x1A4, but it is a lot easier to see the permissions in the octal notation than it is in hexadecimal. (Partly experience, but mainly the appropriate grouping of bits).
Jonathan Leffler
0666 is usually a good setting, even if you don't intend for "other" to be able to write to it. umask is usually set to 0022, which removes write permissions for everyone except the owner.
Jay Conrod
thanks, this is working now.
Chaitanya
+3  A: 

Reading http://linux.die.net/man/2/open it seems you missed the mode parameter for open:

mode must be specified when O_CREAT is in the flags, and is ignored otherwise. The argument mode specifies the permissions to use in case a new file is created.

ZeissS
oh. thanks a lot!
Chaitanya