J
jseb
Hello,
I'd like to bufferise stdout. That's it, when something is sent to
stdout FD, it goes instead to a buffer.
(it's for sending this buffer to another computer with network sockets).
I read that "open_memstren" could do that (posix function).
Here is my test:
///////////// CODE START /////////////////
#include<stdio.h>
#include<malloc.h>
#include<unistd.h>
int main(void)
{
char *buffer; //for open_memstream
size_t size;
FILE *memstream = open_memstream(&buffer, &size);
int stdout_copy = dup(1); //save stdout
stdout = memstream; //redirection
printf("will be put to the buffer");
printf("will be print on stdout!\n");
printf(" another one bufferised");
fclose(memstream);
stdout = fdopen(stdout_copy ,"w+");
printf("stream: %s , size: %d\n", buffer,(int)size);
free(buffer);
return 0;
}
///////////// CODE END /////////////////
It works, except that all strings terminated with "\n" are sent directly
to the terminal !
If you launch this program, you get:
$ ./a.out
will be print on stdout!
stream: will be put to the buffer another one bufferised , size: 48
Do you have any idea about this strange behaviour ?
Thank you.
I'd like to bufferise stdout. That's it, when something is sent to
stdout FD, it goes instead to a buffer.
(it's for sending this buffer to another computer with network sockets).
I read that "open_memstren" could do that (posix function).
Here is my test:
///////////// CODE START /////////////////
#include<stdio.h>
#include<malloc.h>
#include<unistd.h>
int main(void)
{
char *buffer; //for open_memstream
size_t size;
FILE *memstream = open_memstream(&buffer, &size);
int stdout_copy = dup(1); //save stdout
stdout = memstream; //redirection
printf("will be put to the buffer");
printf("will be print on stdout!\n");
printf(" another one bufferised");
fclose(memstream);
stdout = fdopen(stdout_copy ,"w+");
printf("stream: %s , size: %d\n", buffer,(int)size);
free(buffer);
return 0;
}
///////////// CODE END /////////////////
It works, except that all strings terminated with "\n" are sent directly
to the terminal !
If you launch this program, you get:
$ ./a.out
will be print on stdout!
stream: will be put to the buffer another one bufferised , size: 48
Do you have any idea about this strange behaviour ?
Thank you.