1. setvbuf的使用方法
else
{ //fullybuffered
//根據man setvbuf 的解釋,系統會自動設定malloc然後設定buffer
/*
Except for unbuffered files, the buf argument should
point to a buffer at least size bytes long; this
buffer will be used instead of the current buffer.
If the argument buf is NULL, only the mode is af‐
fected; a new buffer will be allocated on the next read
or write operation.
*/
printf("unbuffered\n");
int buff_size = num;
//char *buf1 = (char *)malloc(buff_size);
//char *buf2 = (char *)malloc(buff_size);
assert(setvbuf(input, NULL, _IOFBF, buff_size) == 0);
assert(setvbuf(output, NULL, _IOFBF, buff_size) == 0);
}
2. fprintf的使用方法類似printf,要印出字串的話,在該字串後面一定要補上'\0',這樣才是標準的C字串
if (inputChar == ' ')
{
if (linePos + buflen > 80)
{
fprintf(output, "\n");
total += linePos;
//printf("total:%d\t", total);
linePos = 0;
}
word_buf[buflen] = ' ';
//printf("%d, ", buflen);
//🐉 🐲 🌵 🎄 🌲 🌳 🌴
//在該字的後面加入C字串的結束符號'\0'
//🐉 🐲 🌵 🎄 🌲 🌳 🌴
//word_buf[buflen + 1] = '\0';
fprintf(output, "%s", word_buf);
linePos += buflen+1;
//printf("buflen:%d\t", buflen);
buflen = 0;
continue;
}