[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <stdlib.h>
void pr_stdio(const char *, FILE *);
int main(void)
{
FILE *fp;
fputs ("enter any character\n", stdout);
if (getchar() == EOF) {
perror("getchar");
exit (EXIT_FAILURE);
}
fputs("one line to standard error\n", stderr);
pr_stdio("stdin", stdin);
pr_stdio("stdout", stdout);
pr_stdio("stderr", stderr);
if ((fp = fopen("/etc/issue", "r")) == NULL) {
perror("fopen");
exit (EXIT_FAILURE);
}
if (getc(fp) == EOF) {
perror("getc");
exit (EXIT_FAILURE);
}
pr_stdio("/etc/issue", fp);
return (0);
}
void pr_stdio (const char *name, FILE *fp)
{
printf ("stream = %s, ", name);
if (fp->_IO_file_flags & _IO_UNBUFFERED)
printf("unbuffered");
else if (fp->_IO_file_flags & _IO_LINE_BUF)
printf("line buffered");
else
printf("fully bufferd");
printf(", buffer size = %d\n", fp->_IO_buf_end - fp->_IO_buf_base);
}
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int glob = 6;
char buf[] = "a write to stdout\n";
int main(void)
{
int var;
pid_t pid;
var = 88;
if (write(STDOUT_FILENO, buf, sizeof(buf)-1) != sizeof(buf) - 1) {
perror ("write");
exit (EXIT_FAILURE);
}
printf ("before fork\n");
if ((pid = fork()) < 0) {
perror ("fork");
exit (EXIT_FAILURE);
} else if (pid == 0) {
glob++;
var++;
} else {
sleep(2);
}
printf ("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
exit(0);
}