c++file-pointer

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


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:

void my_function()
{
  FILE* nothing = NULL; // NULL is not correct.
  int n = fprintf(nothing, "PI = %f", 3.14); // Note: When nothing = NULL, error ocured.
}

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


Solution

  • No.

    Your code will cause a run-time exception. You can use /dev/null for example, if you're running on an OS that supports it, but there's nothing like that built-in in C++.