Printing from a buffer using the read(2) function in C -
i'm trying read in bits using read function , i'm not sure how i'm supposed printf results using buffer.
currently code fragment follows
char *infile = argv[1]; char *ptr = buff; int fd = open(infile, o_rdonly); /* read */ assert(fd > -1); char n; while((n = read(fd, ptr, size)) > 0){ /*loops reads file until returns empty */ printf(ptr); }
the data read ptr
may contain \0
bytes, format specifiers , not \0
terminated. reasons not use printf(ptr)
. instead:
// char n; ssize_t n; while((n = read(fd, ptr, size)) > 0) { ssize_t i; (i = 0; < n; i++) { printf(" %02hhx", ptr[i]); // on older compilers use --> printf(" %02x", (unsigned) ptr[i]); } }
Comments
Post a Comment