Solved

Handling stdout

Posted on 2000-03-31
11
420 Views
Last Modified: 2010-04-22
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.
Thanks
0
Comment
Question by:mateif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 10

Expert Comment

by:rbr
ID: 2672559
man freopen
0
 

Author Comment

by:mateif
ID: 2672647
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.
0
 
LVL 10

Expert Comment

by:rbr
ID: 2672656
So why want you to close it. If you just want to flush buffers use fflush (stdin);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:rbr
ID: 2672658
Sorry use
fflush (stdout);
0
 

Author Comment

by:mateif
ID: 2672757
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.
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2675943
I think that rbr's suggestion is what you need. Try flushing the buffers and it should push the data out.
0
 

Author Comment

by:mateif
ID: 2679032
Flushing the buffer has no effect. I tried.
0
 
LVL 10

Expert Comment

by:rbr
ID: 2679301
Can you post your code pls. I'm not sure what your problem is.
0
 
LVL 2

Expert Comment

by:fremsley
ID: 2679683
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.
0
 
LVL 3

Expert Comment

by:alien_life_form
ID: 2680250
Greetings.

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).

Cheers,
    alf
0
 
LVL 1

Accepted Solution

by:
zx9r earned 100 total points
ID: 2735128
do the following steps:

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

2. close stdout
      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);
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
what does daemon command do in linux 3 84
„dd“ vs „ddrescue“ 28 261
Problem to run file 9 130
rolling back version of firefox 1 29
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question