C language library function — error message reporting function

foreword

This article introduces the error message reporting function
The function of the error message reporting function: to help programmers quickly locate errors in the code, so that they can debug and fix problems faster.


1. Error message reporting function

What is the error message reporting function

The error message reporting function is a function in software programming, which is mainly used to process and report error and exception information when the program is running. When an error or abnormal situation occurs when the program is running, the error information reporting function will capture the error information, and then output the error information to the console, log file or other specified targets to help developers find out the problems in the program and correct them. debugging. Usually, during the development process, using the error message reporting function can effectively reduce the time of program error and speed up error repair.

When the library function is executed, if a misplacement occurs, an error code will be stored in the variable errno, where errno is a global variable provided by the C language.

The role of the error message reporting function

**The error message reporting function can help developers quickly detect errors in programs, and better diagnose and solve problems. **By logging and reporting error messages, developers can understand the health of the program, identify possible problems, and debug them more effectively. Additionally, the error message reporting function helps developers track down where, what type, and why an error occurred, and provides useful debugging information for faster error resolution.

strerror

Function introduction

The strerror function is a C standard library function used to convert the value of the errno variable to the corresponding error message string. errno is a global variable that is set to a non-zero value when an error occurs. The prototype of the strerror function is as follows:

char *strerror(int errnum);

Its parameter errnum is usually the value of errno. The strerror function returns a pointer to the error message string. For example, if errno is EACCES, strerror(errno) returns the string "Permission denied".

The strerror function is typically used to print error messages or write error messages to a log file. It helps programmers better understand errors in their programs. The strerror function is a C standard library function used to convert the value of the errno variable to the corresponding error message string. errno is a global variable that is set to a non-zero value when an error occurs. The prototype of the strerror function is as follows:

The strerror function uses

/* strerror example : error list */
#include <stdio.h>
#include <string.h>
#include <errno.h>//必须包含的头文件
int main ()
{
    
    
 FILE * pFile;
  pFile = fopen ("unexist.ent","r");
  if (pFile == NULL)
    printf ("Error opening file unexist.ent: %s\n",strerror(errno));
    //errno: Last error number
  return 0;
}
Edit & Run

The error message corresponding to the error code

In the vs compiler, you can view it through the following code

#include<stdio.h>
#include<errno.h>
#include <string.h>
int main()
{
    
    
	int i = 0;
	for (i = 0; i < 10; i++)
	{
    
    
		printf("%d : %s\n", i,strerror(i));
	}
	return 0;
}

output result
insert image description here


The following are common C language error codes and their corresponding error messages:

请注意,这仅是一些常见的错误码,实际情况可能因系统和环境而异。

  • 1:Operation not permitted
  • 2:No such file or directory
  • 3:No such process
  • 4:Interrupted system call
  • 5:I/O error
  • 6:No such device or address
  • 7:Argument list too long
  • 8:Exec format error
  • 9:Bad file number
  • 10:No child processes
  • 11:Try again
  • 12:Out of memory
  • 13:Permission denied
  • 14:Bad address
  • 15:Block device required
  • 16:Device or resource busy
  • 17:File exists
  • 18:Cross-device link
  • 19:No such device
  • 20:Not a directory
  • 21:Is a directory
  • 22:Invalid argument
  • 23:File table overflow
  • 24:Too many open files
  • 25:Not a typewriter
  • 26:Text file busy
  • 27:File too large
  • 28:No space left on device
  • 29:Illegal seek
  • 30:Read-only file system
  • 31:Too many links
  • 32:Broken pipe

如这篇博客对大家有帮助的话,希望 三连 支持一下 !!! 如果有错误感谢大佬的斧正 如有 其他见解发到评论区,一起学习 一起进步。

Guess you like

Origin blog.csdn.net/m0_74014525/article/details/131704479