Is there a file pointer (FILE*) that points to nothing?

前端 未结 6 726
醉酒成梦
醉酒成梦 2021-01-23 03:09

For some reasons I need a file pointer (FILE*) that points to nothing. It means I can pass it to fprintf function and fprintf ignore the file pointer.

for example:

6条回答
  •  [愿得一人]
    2021-01-23 03:36

    Although as NirMH said, you can enclose it in if (nothing != NULL), there is another way. You can open a file in read mode (with "r") and when you send it to fprintf, the write is ignored (Edit: as discussed in the comments, remember to set n=0 if it was negative as returned by fprintf).

    I personally suggest the first method though. The only reason I might do the second is if I can't change the function.

    If you don't care if your code is system dependent, you can use /dev/null in linux, nul in windows etc,

提交回复
热议问题