I am trying to write data in C with fputs() and would need to get an error when the destination doesn't exist.
This is my code:
And this is the output:
And here the program just disappears without reaching exit() and without a message.
How can I catch the error condition in code?
This is my code:
Code:
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <regex.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *wp;
int i, j;
if((wp = popen("/usr/crap/bogus", "w")) == NULL) {
printf("popen failed\n");
exit(1);
}
printf("popen okay\n");
for(i = 1; i < 1000; i++)
if((j = fputs("crapcrapcrap\n", wp)) == EOF) {
printf("fputs error: EOF\n");
exit(1);
} else
printf("fputs #%d okay %d chars\n", i, j);
printf("pclose: \n");
i = pclose(wp);
printf("pclose result=%d\n", i);
exit(0);
}
And this is the output:
Code:
$ ./a.out
popen okay
fputs #1 okay 13 chars
fputs #2 okay 13 chars
fputs #3 okay 13 chars
fputs #4 okay 13 chars
...
fputs #31 okay 13 chars
fputs #32 okay 13 chars
fputs #33 okay 13 chars
sh: /usr/crap/bogus: not found
fputs #34 okay 13 chars
fputs #35 okay 13 chars
fputs #36 okay 13 chars
...
fputs #312 okay 13 chars
fputs #313 okay 13 chars
fputs #314 okay 13 chars
fputs #315 okay 13 chars
$
And here the program just disappears without reaching exit() and without a message.
How can I catch the error condition in code?