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

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -