0
votes

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.

2
One obvious possibility would be an attempt at writing to a disk that's full (and doesn't have write-back caching).Jerry Coffin
Oh I didn't think of that. Any other possibilities that I can test more easily?Austin
You could close stdout.Kerrek SB
@KerrekSB: That would be UB though. The value to a pointer to a FILE object is indeterminate after it is closed.dreamlax
@Jake: In response to your side question, if you cout << EOF << endl; it will simply print a negative number, e.g. -1. Functions that return EOF typically have the return type int, so it simply calls the << overload for int.dreamlax

2 Answers

4
votes

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.

0
votes

Unfortunately, this doesn't work. Linux pipes, except for "stderror" are buffered. "puts" won't give an error message (-1) using the example above.