If I use printf ("Hello!");
are there any ways to read what I've printed into stdout?
I saw C language. Read from stdout (it uses pipe) but it doesn't work:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <unistd.h>
#define DIM 10
int main(void)
{
char s[DIM];
int fds[2];
pipe(fds);
dup2(fds[1], fileno(stdout)); //I added fileno()
printf("Hello world!");
read(fds[0], s, DIM);
printf("\n%s",s);
return 0;
}
I also tried using a file descriptor but neither it doesn't work:
#define _XOPEN_SOURCE
#include <stdio.h>
#include <unistd.h>
#define DIM 10
#define MYFILE "/path/file"
int main(void)
{
FILE *fd;
char s[DIM];
fd = fopen(MYFILE,"w+");
dup2(STDOUT_FILENO, fileno(fd));
printf("Hello world!");
fseek(fd, 0, SEEK_SET);
fgets(s, DIM, fd);
printf("\n%s",s);
return 0;
}
How can I do?