Question

I've got an array of characters which could contain a NULL character.
The array is not null-terminated, but I do know it's length.

Question
How do I output this exact array (including null characters) and nothing else (no trailing \n) to stdout? Unfortunately, I am not allowed to output one character at a time.

Was it helpful?

Solution

char buffer[SIZE];

if(fwrite(buffer, 1, SIZE, stdout) != SIZE) {
    perror("fwrite");
}

OTHER TIPS

This should do the trick

size_t rt = write(STDOUT_FILENO, &yourCharArray, SIZE_OF_YOUR_ARRAY);

where rt is the number of the actual written bytes

Though hard to find if you're a C newbie, the answer turns out to be very easy.

C sees stdout as a file, which means you can simply use fwrite to accomplish this.

fwrite(CHAR_ARRAY, sizeof(char), NR_CHARS, stdout);

Here you go:

char arr[SIZE]
fwrite(arr, sizeof(char), SIZE, stdout)

fwrite is in stdio.h

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top