c++puts

When will puts() fail?


For puts (const char*), I read that, "On success, a non-negative value is returned. On error, the function returns EOF and sets the error indicator (ferror)."

I'm trying to get the function to error so I can cout the EOF return, but I can't seem to. If I don't initialize the char* to anything, or set it to NULL, it still returns a non-negative success value. What is something that would actually make this function fail? Thanks.

Side question: If you cout << a function that's supposed to return EOF, will it actually print 'EOF', something else, or nothing? This is actually what I was trying to test in the first place.


Solution

  • If you're on a system that supports it (e.g., Linux), you can redirect your program's output to /dev/full, a device that yields an infinite stream of zero bytes on input and fails with ENOSPC ("No space left on device") on output.

    ./program > /dev/full
    

    Of course you'll have to print any error messages to stderr (or std::cerr) or to a file.