If I set lseek to say an offset of 20000 after the END of file, why can't I write the string "hello" at this position??
Code:
/*if you read data after end of file, buffer displays NULL*/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFFERSIZE 4096
int main(int ac, char *argv[])
{
int fd1;
ssize_t nread,nwrite,nbytes ;
off_t newpos;
char buf[BUFFERSIZE];
fd1 = open("b.txt", O_RDWR);
if (fd1 == -1)
perror("error");
//offset 20000 positions after end of file
newpos = lseek(fd1, 20000, SEEK_END);
nread = (fd1,buf,20);
printf("buffer contains: %s\n", buf);
strcpy(buf, "hello");
nbytes = strlen(buf);
printf("buffer contains: %s\n", buf);
nwrite = (fd1,buf,20);
if (nwrite == -1)
perror("error here");
}