Error using 'read', error 9. I must be blind

Hi. I'm a bit desesperated, Can you help me?

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#define SIZE 1024

extern int error;

int main () {
        int file = open("lets.try", O_WRONLY|O_CREAT|O_TRUNC, 0666);

        unsigned char buffer[SIZE];
        memset (&buffer, 0xFF, SIZE);

        write(file, buffer, SIZE);

        lseek (file, SEEK_SET, 0);
        if (read(file, buffer, SIZE) == -1)
                printf ("\nERROR == %d :(\n\n", errno);

        close(file);
}

When this code is executed a file named "lets.try" is created with size == 1KB and plenty of '1's. But also the if condition is "trued", so it means there's have been an error reading the file. But... why? Why writing works and reading not? What am I missing?.

I've used errno variable to try to guess what's happening. As i've read value 9 means:

EBADF
fd is not a valid file descriptor or is not open for reading.

But this has no sense because then that same file descriptor would be worng for write operation.

Mm.. I wish you can help me. I'm sure it's something stupid but I can't see it.

I've build the code in knoppix, xfld and Kubuntu. (All live versions). All are based on debian and same error on all.

Cheers
KyroiiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brettmjohnsonCommented:
>  int file = open("lets.try", O_WRONLY|O_CREAT|O_TRUNC, 0666);

O_WRONLY  means "WRITE ONLY".  The file is opened for writing only, not reading.
If you want to both read and write the file, use O_RDWR rather than O_WRONLY.



praseedgopalCommented:
Hi,

    int file = open("lets.try", O_WRONLY|O_CREAT|O_TRUNC, 0666);

   The file lets.try has been opened in write-only mode (O_WRONLY). It's not opened for reading. So you have to change O_WRONLY with O_RDWR.
    Or you can close the file and open again for read-only(O_RDONLY).

wishes

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
praseedgopalCommented:
oops ...
  so its already answered.
KyroiiAuthor Commented:
Damn! So easy... I knew it was something stupid.

Thk u all.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.