c - Printing out a char[] -
i cannot life of me remember how this. program opens file reads file. print out contents has read.
int main(int argc, char *argv[]) { char memory[1000]; //declare memory buffer size int fd = 0; int count = 1000; if ((fd = open(argv[1], o_rdonly)) == -1) { fprintf(stderr, "cannot open.\n"); exit(1); } read(fd, memory, count); //printf buffered memory contents return 0; }
printf
accepts %s
format print c-string. however, default requires string have null-terminator (0x0 ascii code). if sure read call read
can this:
printf("%s\n", memory);
however, cannot sure. because don't check how many bytes read... or error code. have fix code first.
once done checking errors , know how many bytes read, can this:
printf("%.*s\n", (int)bytes_that_were_read, memory);
good luck!
Comments
Post a Comment