reading a file that doesn't exist

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2011-02-19T07:18:54Z Indexed on 2011/02/19 7:25 UTC
Read the original article Hit count: 224

Filed under:
|
|

Hi,

I have got a small program that prints the contents of files using the system call - read.

unsigned char buffer[8];
size_t offset=0;
size_t bytes_read;

int i;

int fd = open(argv[1], O_RDONLY);

do{
    bytes_read = read(fd, buffer, sizeof(buffer));
    printf("0x%06x : ", offset);

    for(i=0; i<bytes_read; ++i)
    {
        printf("%c ", buffer[i]);
    }
    printf("\n");
    offset = offset + bytes_read;
}while(bytes_read == sizeof(buffer));

Now while running I give a file name that doesn't exist. It prints some kind of data mixed with environment variables and a segmentation fault at the end.

How is this possible? What is the program printing?

Thanks, John

© Stack Overflow or respective owner

Related posts about c

    Related posts about unix