i m reading the book "linux system programming" and i get a little confused with read function.
here is the code from the book.
so why to read 4 bytes (unsigned long) when char is 1 byte? because if we read a file, everything in it will be character.Code:unsigned long word;
ssize_t nr;
/* read a couple bytes into 'word' from 'fd' */
nr = read (fd, &word, sizeof (unsigned long));
if (nr == -1)
/* error */
another example from the book...
assuming that len again is unsigned long ...
i tried that but it's not working properly, so i needed to modify in my wayCode:
while (len != 0 && (ret = read (fd, buf, len)) != 0) {
if (ret == -1) {
if (errno == EINTR)
continue;
perror ("read");
break;
}
printf("%c", buf);
len -= ret;
buf += ret;
}
tnxCode:
int len = sizeof(char);
int broj=0;
while((ret = read(fd, &buf, len )) != 0 ){ // read 1 byte (char) and store it in buf
if(ret == -1){
if(errno == EINTR)
continue;
perror("read");
break;
}
printf("%c", buf);
}

