Summary of correlation function usage of return error information in C language _c language

Source: Internet
Author: User
Tags assert error code

C language Strerror () function: A description string that returns the cause of the error
header file:

#include <string.h>

To define a function:

char * strerror (int errnum);

Function Description: Strerror () is used to query the error code of the parameter Errnum for the description string for its error reason, and then returns the string pointer.

Return value: Returns a string pointer that describes the cause of the error.

Example:

/* Displays error code 0 to 9 for error reason description
/* #include <string.h>
main ()
{
  int i;
  for (i = 0; i < i++)
  printf ("%d:%s\n", I, strerror (i));
}

Perform:

0:success
1:operation not permitted
2:no such file or directory
3:no such process
4:interrup Ted System call
5:input/output error
6:device not configured
7:argument list too long
8:exec F Ormat Error
9:bad file descriptor

C language Perror () function: Print the most recent system error message
the function perror () is used to throw the most recent system error message, with the following prototype:

  void Perror (char *string);

The "argument" string is the error message to output.

Description: Perror () is used to output the error to the standard error (STDERR) for the last function. The string that the argument string refers to is printed first, followed by the error reason string, which determines the string to output according to the value of the global variable errno.

There is a errno variable in the library function, and each errno value corresponds to the type of error expressed as a string. When you invoke the "certain" function, the function has reset the errno value. The perror function simply outputs some of the information you have entered along with the error of the current errno.

"Instance" opens a nonexistent file and prints an error message.

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
int main (void)
{
  FILE *FP;
  fp = fopen ("Test.txt", "w");/* Open File/
  assert (FP);/* Assertion NOT NULL/
  fclose (FP);/* Close/
  fp = fopen ("Nulltest.tx T "," R ");/* Open a nonexistent file *
  /if (NULL = fp)
  {/
    * display last error message
    /perror (" fopen "(" nulltest.txt\ ", \" R\ ") ");
  }
  return 0;
}

Run Result:

fopen ("Nulltest.txt", "R"): No such file or directory

The program first defines a file pointer fp, then creates a file Test.txt, asserts that the file was opened successfully, then closes the file, opens the file nulltest.txt as read-only, determines whether the file pointer is empty, and uses perror () to output the error message if it is empty. Perror () is used to output the error of the previous function to the standard device (stderr). The string that the function argument string refers to is printed first, followed by the error reason string. The reason for this error is to determine the string to output according to the value of the global variable error.

If you call perror continuously, the following:

Perror ("1");

Perror ("2");

The following results are present:

1:success

2:illegal Seek

This problem arises because:

Perror () is the Lseek method to find the corresponding error message according to Errorno, and then output.

When the program is running, the Errorno's initial value corresponds to the information: Success. Only when there are errors in the program will change to Errorno. If there are no errors, the value of the Errorno will not change.

However, the Perror method is an exception, each time the Perror method is invoked, it will first output the errorno corresponding information, and then reset Errorno to undefined, if you immediately call perror, Perror in the Lseek according to undefined to find the wrong message, will be thrown wrong, the error is just recorded in the Errorno, therefore, the Lseek of the error printed out. So the first call to the Perror method will be normal data error information, but if the second consecutive call, because the first perror call has been errorno set to undefined, so the second consecutive, the third time ... The 100 millionth call will output a illegal seek error.

C language Ferror () function: Check the file stream for errors to occur

Header file:

#include <stdio.h>

To define a function:

int ferror (FILE *stream);

Function Description: Ferror () is used to check whether the file stream specified by the parameter stream has an error condition, and returns a value other than 0 if an error occurs.

Return value: Returns a value other than 0 if there is an error in the file stream.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.