[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Midlertidig udskrift til stderr



On Sun, Apr 13, 2003 at 05:30:39PM +0200, Jan Eggert Kofoed wrote:
> Jeg har brug for at lave redirection af stderr til en fil,
> som jeg har åbnet. Det virker sådan fint nok:
> 
>   string sErr = "TestCscanInput.err";
>   ofstream oError(sErr.c_str());
>   cerr = oError;
>   ...
>   oError.close();
> 
> Mit problem er at få stderr tilbage igen!
> 
> Hvordan kan jeg lave en midlertidig redirection af stderr?
På et lidt lavere niveau kan du rode med at kopiere fildescriptorere
rundt. Det ud gør er noget i stil med flg (som ikke er testet, og som
heller ikke laver nogen form for fejl check).

Start af redirection:

int oldstderr = dup(2);
int newstderr = open("fil.log", O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
dup2(2, newstderr);
close(newstderr);

Efter den dup2, vil fildescriptor 2 (som er stderr) pege på din fil.

Stop af redirection:

dup2(2, oldstderr)
close(oldstderr);

Og så vil disse stumper kode også virke med C og hvad du ellers kunne
finde på af underlige ting.

Ellers kunne du jo bare tildele den oprindelige værdi af cerr til en
variabel i begyndelsen af programmet.

-- 
/-----------------------------------------------------\
| Klaus S. Madsen      | "Failure is not an option... |
| ICQ: 45400164        |  It comes bundled with your  |
| www.hjernemadsen.org |  Microsoft products!"        |
\-----------------------------------------------------/


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *