2016-03-29 32 views
-1

Windows platformu için buna eşdeğer lazım, herhangi bir işaretçi lütfen? Bu benim * Nix platformları için takip ne olduğunu ve (eğer error_stream diyebiliriz?) Bir değişken ilan etmeyi düşünebilir, daha genişletilebilir alternatif olarak heregeçici yönlendirme stderr.

+1

Windows için benzer olmalıdır, ancak #include 'zorunda' ve yerine dup2' vb –

+1

Ah, 'arasında _dup2'' yerine dup' 'arasında _dup'' kullanmalı ve '/ dev/null' 'yerine' 'nul'' açmanız gerekir. –

cevap

1

bulabilmesi far.The linki çalışıyor görünüyor hangi stderr olarak ayarlamış olduğunuz süreler ve diğer zamanlarda başka bir dosyaya (örneğin, Windows NT'de fopen(NUL_DEVICE_FILENAME, "wb");) ayarladınız.

Bu kodun güzel bir yönü, her OS için NUL_DEVICE_FILENAME (hatta tüm işlevlerin) değiştirilebilmesidir; işlevler, bağlantı noktasına daha az taşınabilir davranışı kolaylaştırmak için bir arabirim haline gelir. Örnek olarak kullanım için test.c'ye (bu yazının en altına yakın) bakın ve bunun çıktısı çalışmanın kanıtıdır. Bu pasajı iyi şanslar ... :)

error_stream.h:

#ifndef INCLUDE_ERROR_STREAM 
#define INCLUDE_ERROR_STREAM 
#include <stdio.h> 

FILE *get_error_stream(void); 
void set_error_stream(FILE *); 
void reset_error_stream(void); 
void blank_error_stream(void); 
#endif 

error_stream.c:

#include "error_stream.h" 
#define NUL_DEVICE_FILENAME "NUL" /* This worked fine for me on Win10 */ 
            /* Try "\\Device\\Null", "NUL" and * 
            * ... "NUL:" if it doesn't work, * 
            * ... or obviously "/dev/null" on * 
            * ... *nix      */ 
FILE *error_stream, *blank_stream; 
FILE *get_error_stream(void) { 
    if (!error_stream) { 
     error_stream = stderr; 
    } 
    return error_stream; 
} 
void set_error_stream(FILE *f) { 
    error_stream = f; 
} 
void reset_error_stream(void) { 
    set_error_stream(stderr); 
} 
void blank_error_stream(void) { 
    if (!blank_stream) { 
     blank_stream = fopen(NUL_DEVICE_FILENAME, "wb+"); 
    } 
    set_error_stream(blank_stream); 
} 

test.c:

#include "error_stream.h" 
int main(void) { 
    fputs("Testing\n", get_error_stream()); 
    blank_error_stream(); 

    fputs("Testing\n", get_error_stream()); 
    reset_error_stream(); 

    fputs("One\n", get_error_stream()); 
    blank_error_stream(); 

    fputs("Two\n", get_error_stream()); 
    reset_error_stream(); 
} 

C:\Users\Seb\Desktop>gcc -c error_stream.c -o error_stream.o 
C:\Users\Seb\Desktop>gcc test.c error_stream.o -o test.exe 
C:\Users\Seb\Desktop>test 
Testing 
One