简体   繁体   中英

How can I get the internal error message to log?

I'm using perror:

perror("Error message:");

When the above is used it displays the message passed to the string followed by the actual error that occurred. This is a standard function, however I want to log this to a file, how to I get the actual error that goes with this so I can log it?

You can use the code below:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>


int main() {
    FILE* filePtr;
    filePtr  = fopen (logFilePath, "a");
    //...
    //some code
    //...
    
    if(critical_part){
        fprintf(filePtr, "errno :%s Error in critical part - line 123\n",strerror(errno));
    }
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM