Rather than use fread()/fwrite()
to read in text, use fgets()
to read and fprintf()
to justify.
assert(w >= 0);
while (fgets(buf, TEMP, f_open) != NULL) {
// right justify the text
fprintf(stdout, "%*s", w + 1, buf);
}
The + 1
is for the usual \n
at the end of buf
. A bit more work is needed to deal with the case when \n
is not there. e.g. last line of a file.