5

有没有办法获得一个FILE*没有指向的 C 流对象(一个对象)?

我知道fopen("/dev/null","w");会起作用,但我想知道是否有更好的方法。

优选地,比特将数据存储在比posix层更高的级别,并且也更便携。

4

2 回答 2

10

否:/dev/null在 Unix 和NUL:Windows 上(在没有 Cygwin 或同等产品的情况下)是最好的方法。

提到的问题的原始版本,fopen("/dev/null","o");但此后已被修复。
哦,"o"标志 tofopen()是不可移植的。可移植形式包括各种组合的标志字符r, w, a, b, +

于 2009-12-13T01:23:10.390 回答
6

I have some logging in place that goes to stderr and I want to be able to turn it off with a flag. I'd really rather not have to do more to it than change the variable that gets passed to fprintf

a) Wrapper function

logging = TRUE;
void debugprint(...)
{
    if (logging)   
    {
        fprintf(stderr, ...);
    }
}

b) I think fprintf will return if you give it a null pointer. Can't remember -- give it a try. Then all you have to do is change the pointer :)

于 2009-12-13T01:39:22.893 回答