c - Why is my function representation not reverse-copying data? -
c - Why is my function representation not reverse-copying data? -
i have function representation of fwrite
write bytes backwards. however.. doesn't re-create them backwards appears strange.
size_t fwrite_backward ( const void * ptr, size_t size, size_t count, file * stream ) { int i, chr; for(i = count * size; != 0; i--) { if( fputc(*(unsigned char*)ptr + (count * size) - i, stream) != eof ) chr++; } homecoming chr; }
it supposed behave fwrite
2 differences:
ferror
what might doing wrong?
the implementation below seems work. corrects minor design flaws of version:
your code writes out bytes backwards, should write items backwards bytes in items in original order. (after all, what's distinction between size
, count
for?) code below works items of size.
yor code returns number of bytes written. fwrite
returns number of items written.
the loop ends on unsuccessful writes.
so:
size_t fwrite_rev(const void *ptr, size_t size, size_t count, file *stream) { const unsigned char *p = ptr; size_t written = 0; while (count--) { if (fwrite(p + size * count, size, 1, stream) < 1) break; written++; } homecoming written; }
c io fwrite
Comments
Post a Comment