Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
I thought I would check errno after a long string of printf calls ...
I thought I'd check errno after a long string of printf calls, to see if any of them had failed:
errno = 0;
printf("This\n");
printf("is\n");
printf("a\n");
printf("test.\n");
if(errno != 0)
fprintf(stderr, "printf failed: %s\n", strerror(errno));
Why is it printing something strange like ``printf failed: Not a typewriter'' when I redirect the output to a file?
✍: Guest
Many implementations of the stdio package adjust their behavior slightly if stdout is a terminal. To make the determination, these implementations perform some operation which happens to fail (with ENOTTY) if stdout is not a terminal. Although the output operation goes on to complete successfully, errno still contains ENOTTY. This behavior can be mildly confusing, but it is not strictly incorrect, because it is only meaningful for a program to inspect the contents of errno after an error has been reported. (More precisely, errno is only meaningful after a library function that sets errno on error has returned an error code.)
In general, it's best to detect errors by checking a function's return value. To check for any accumulated error after a long string of stdio calls, you can use ferror.
2015-10-14, 920👍, 0💬
Popular Posts:
Where are cookies actually stored on the hard disk? This depends on the user's browser and OS. In th...
How To Select Some Rows from a Table? - MySQL FAQs - SQL SELECT Query Statements with GROUP BY If yo...
What is the difference between "calloc(...)" and "malloc(...)"? 1. calloc(...) allocates a block of ...
How To Delete All Rows a Table? - MySQL FAQs - Understanding SQL INSERT, UPDATE and DELETE Statement...
How To Blend a Color Layer to a Image? - PSP Tutorials - Fading Images to Background Colors with PSP...