Originally Posted by
whiteflags
Are you sure the argument should be &buff? That's different from both buff and &buff[0].
I think &buff is actually the same as buff here (what else could it mean?). Still, it's weird.
OP, if you look more closely you'll see that it's printing the first char correctly. However, you are asking it to put every character into the first byte of buff, but are printing out the successive bytes of buff (buff[count], which will contain arbitrary "garbage"). Try:
Code:
read(fd, &buff[count], 1) // sizeof(char) is always 1
Of course, as whiteflags said, there's not much point in reading a byte at a time.
And if it's outputing "nothing at all", that may be a space or newline character. You can use isprint to see if a character is printable.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char *argv[]){
if (argc != 2){
fprintf(stderr, "Error: Enter a single text file name.\n");
exit(EXIT_FAILURE);
}
char buff[1000];
int count = 0;
int fd = open(argv[1], O_RDONLY);
for ( ; read(fd, &buff[count], 1) > 0; count++)
printf("%4d: %c\n", count,
isprint(buff[count]) ? buff[count] : '.');
close(fd);
return 0;
}