#include <stdio.h>
#include <stdlib.h>
void mysetbuf(FILE *__restrict fp, char *__restrict buf);
int main()
{
FILE *fp;
char buf[BUFSIZ];
if ((fp = fopen("test.mayjie","a+")) == NULL)
{
printf("fopen error!/n");
exit(-1);
}
mysetbuf(fp,buf);
mysetbuf(stdin,buf);
mysetbuf(stdout,buf);
mysetbuf(stderr,buf);
fclose(fp);
return 0;
}
void mysetbuf(FILE *__restrict fp, char *__restrict buf)
{
if (buf == NULL)
{
if (setvbuf(fp,NULL,_IONBF,0) != 0) /* 无缓冲 */
{
printf("setvbuf error!/n");
exit(-1);
}
}
else
{
if (fp == stderr)
{
if (setvbuf(fp,NULL,_IONBF,0) != 0) /* 无缓冲 */
{
printf("setvbuf error!/n");
exit(-1);
}
}
else if (fp == stdin || fp == stdout)
{
if (setvbuf(fp,buf,_IOLBF,BUFSIZ) != 0) /* 行缓冲 */
{
printf("setvbuf error!/n");
exit(-1);
}
}
else
{
if (setvbuf(fp,buf,_IOFBF,BUFSIZ) != 0) /* 全缓冲 */
{
printf("setvbuf error!/n");
exit(-1);
}
}
}
}
UNIX环境高级编程习题 5.1
最新推荐文章于 2022-12-23 22:17:00 发布