• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

Handling stdout

In a C program, I want to close the file stream associated with stdout [ something like fclose(stdout);] and open it again. Who can tell me how can I do this?
I use Linux and gcc.
1 Solution
man freopen
mateifAuthor Commented:
What I want to do is to reopen the stream and associate it with stdout again. Using freopen would require a first argument ( char *path) and I don't know which would that be. I tried NULL and it didn't work out.
So why want you to close it. If you just want to flush buffers use fflush (stdin);
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Sorry use
fflush (stdout);
mateifAuthor Commented:
What I want is to display two pages with a cgi, without stopping the cgi. The two pages are constructed by the cgi, and i don't want to save them on the hard-disk. So I saw that if i close stdout it displays the first page, so i need to reopen it to display the second. I tried to input EOF in stdout, but it didn't work.
I think that rbr's suggestion is what you need. Try flushing the buffers and it should push the data out.
mateifAuthor Commented:
Flushing the buffer has no effect. I tried.
Can you post your code pls. I'm not sure what your problem is.
have you tried to write a formfeed ('\f') or an Unix EOF ('\004')? note: the constant EOF is not a valid character to write to stream.

If I understand correctly what you want to do, the answer is: 'You cannot, because the CGI protocol prevents it'.

You cannot output two CGI pages during the same transaction, because the web server will just leave and close the client transaction after you're done with the first one. When your CGI starts up, your stdout is connected - through a socket - to the browser. After you're done with the transaction, the browser renders the page and *goes away* - it does not come back looking for seconds.

If persistent/multistep  connections are what you want, you need to go with different techniques (Cookies, hidden fields, php, and/or more). Fiddling with file handles won't buy you anything (regretfully).

do the following steps:

1. copy the file descriptor of the stdout :
       int fd;
       fd = dup(stdout);

2. close stdout

3. do the job
      here is important to not open any file or if you open any, make sure that       you close it before continuing with the next step

4. to restaure stdout you only have to do
      dup2(fd, stdout);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now