How use perror() but output the prompt to a file

android, android-emulator, c, c++, linux

Solution

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).

Problem

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.

Original source

Related problems