I know the general usage of the perror()
.
But right now, I want to export its result into a file, not the console. By the way, i run it in under the android emulator's adb shell.
I know the general usage of the perror()
.
But right now, I want to export its result into a file, not the console. By the way, i run it in under the android emulator's adb shell.
Use strerror()
instead:
fprintf(logfile, "Something went wrong: %s\n", strerror(errno));
Or, you could redirect stderr
to a file using dup2()
and then carry on using perror()
.
EDIT: It was early when I answered this originally and my brain wasn't firing on all cylinders. There is no need to use sprintf()
followed by fputs()
as fprintf()
can do it all (thanks to @maverik).
You can redirect the whole stderr
to that file. Here is a Unix way of doing this:
int fd = open("logfile", O_WRONLY); // 'fd' stands for 'file descriptor
close(2); // close stderr
dup(fd); // duplicate fd so that it's copy will be the lowest available value -- 2
I believe you can direct the stderr to a file of your choosing, e.g $ ./program 2> errlog