当前位置:网站首页>StrError & PERROR use yyds dry inventory

StrError & PERROR use yyds dry inventory

2022-07-06 03:08:00 Mango_ Mango

summary :

strlen - Library function for finding string length

strcpy- String copy function

strcat - character string ( Connect ) Append function

strncpy() - String copy function - Can control the number of characters copied

strncat() - String append function

strncmp() - String comparison function

strstr - String lookup function

strtok - String cutting

memcpy()- Memory copy function

memmove- Memory copy function ( Can handle overlapping memory copies )

memcmp: Memory compare function

memset - Memory setting function

strerror- Can return C The error message corresponding to the error code built in the language

perror - Print custom content +strerror

Error message reporting function

strerror- Can return C The error message corresponding to the error code built in the language

strerror&perror Use #yyds Dry inventory # _ error message


Return the error information corresponding to the error code

Parameter error code , Translate into error message , The first character address of the error message string corresponding to the error code is returned

C When a language library function call fails , Will store the error code in errno variable


#include<string.h>
int main()
{
     printf("%s\n",strerror(0));
     printf("%s\n",strerror(1));
     printf("%s\n",strerror(2));
     printf("%s\n",strerror(3));
    return 0;
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

 Print the results :
No error
Operation not permitted
No such file or directory
No such process

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

application :

int main()
{
    FILE* pf = fopen("test.txt","r");
    if(pf == NULL)
    {
        printf("%s\n",strerror(errno));	//C Will put the error code in errno Variable , Print the error message corresponding to the error code 
        
    }
    else
    {
        printf(" Open the success \n");
    }
    return 0;
}
//fopen(): Functions for opening files , If the opening is successful , Returns a file pointer to the file FILE*  If not, return to NULL
//r: Read only open 

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

Since we don't have this file : So print the results

No such file or directory


perror - Print custom content +strerror

strerror&perror Use #yyds Dry inventory # _ error message _02

string: Custom characters


int main()
{
    FILE* pf = fopen("test.txt", "r");
    if (pf == NULL)
    {
        perror(" test ");
        perror(" test :\n");
        perror("\n");
    }
    else
    {
        printf(" Open the success \n");
    }
    return 0;
}

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

Print the results :

 test : No such file or directory
 test :
: No such file or directory

: No such file or directory

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

If perror There's something in , Print content first , Then automatically add a colon after it : Then print the error message


shortcoming : Print when you don't want to print


strerror&perror Use #yyds Dry inventory # _ Error code _03

原网站

版权声明
本文为[Mango_ Mango]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132334222548.html