perror() Function - example program in C
By: Lakshmi
The perror() function is another of C's error-handling tools. When called, perror() displays a message on stderr describing the most recent error that occurred during a library function call or system call. The prototype, in STDIO.H, is
void perror(char *msg);
The argument msg points to an optional user-defined message. This message is printed first, followed by a colon and the implementation-defined message that describes the most recent error. If you call perror() when no error has occurred, the message displayed is no error.
A call to perror() does nothing to deal with the error condition. It's up to the program to take action, which might consist of prompting the user to do something such as terminate the program. The action the program takes can be determined by testing the value of errno and by the nature of the error. Note that a program need not include the header file ERRNO.H to use the external variable errno. That header file is required only if your program uses the symbolic error constants. Sample program below illustrates the use of perror() and errno for handling runtime errors.
Using perror() and errno to deal with runtime errors.
1: /* Demonstration of error handling with perror() and errno. */ 2: 3: #include <stdio.h> 4: #include <stdlib.h> 5: #include <errno.h> 6: 7: main() 8: { 9: FILE *fp; 10: char filename[80]; 11: 12: printf("Enter filename: "); 13: gets(filename); 14: 15: if (( fp = fopen(filename, "r")) == NULL) 16: { 17: perror("You goofed!"); 18: printf("errno = %d.\n", errno); 19: exit(1); 20: } 21: else 22: { 23: puts("File opened for reading."); 24: fclose(fp); 25: } 26: return(0); 27: } Enter file name: list19_4.c File opened for reading. Enter file name: notafile.xxx You goofed!: No such file or directory errno = 2.
ANALYSIS: This program prints one of two messages based on whether a file can be opened for reading. Line 15 tries to open a file. If the file opens, the else part of the if loop executes, printing the following message:
File opened for reading.
If there is an error when the file is opened, such as the file not existing, lines 17 through 19 of the if loop execute. Line 17 calls the perror() function with the string "You goofed!". This is followed by printing the error number. The result of entering a file that does not exist is
You goofed!: No such file or directory. errno = 2
DO include the ERRNO.H header file if you're going to use the symbolic errors.DON'T include the ERRNO.H header file if you aren't going to use the symbolic error constants.
DO check for possible errors in your programs. Never assume that everything is okay.
Archived Comments
1. Notice that on an UNIX environment (including Linux, Mac and iPhone), the filesystem is case sensiti
View Tutorial By: Alejandro segovia at 2011-02-22 11:08:23
2. Nice example. I think this is more idiomatic tho'.
/* Demonstration of error
View Tutorial By: John Morrison at 2010-03-28 14:55:07
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C