Reason analysis of failure of fopen function and method of analyzing reason __ function

Source: Internet
Author: User
Tags function prototype
Overview:In the recent analysis of an occasional problem, the occasional probability is particularly low, the problem is still in the analysis. Make a summary of the analytical knowledge and continue to add it later.
Problem Description:The code encountered an exception when calling Lua's require function, and by looking at require's source trace, it found that the function's fopen function returned an exception that failed to open the file.
The following is to summarize the fopen open file error may have what reasons, may not be complete, welcome to add.
function Description:Function prototype FILE * fopen (const char * path,const char * mode);
function function to open a file
Parameter: path [in] name Mode[in] Open mode
Return value: After the file is successfully opened, the file pointer to the stream is returned. Returns null if the file fails to open, and the error code is in errno.
Note: Here simply describes the functions and parameters of the function, the specific parameter meaning is not analyzed here.
reason Analysis and Method1 parameter path problem, the path is not fopen will be returned failed.
Analysis: First look at whether the path file exists, and then check the path relative path or absolute path. If it is a relative path and then check if the directory of the current process is switched, the software cannot find the file.
such as relative path FILE *FP = fopen ("./test/1.txt", "R");
Absolute path FILE *FP = fopen ("/mnt/text/1.txt", "R");
If you are sure that the parameter path is OK, you can exclude the path problem where the file does not exist. You can now print the error code errno to locate the problem (described later).

2 parameter mode problem, mode controls how the file is opened, and if the user opens in a way that exceeds the current user's permissions, then fopen will return a failure.
You should check the current user's action permissions, or print the error code errno to locate the problem
Open File *FP = fopen ("./test/1.txt", "w+") if the current user only has Read permission and reads and writes

3 Check whether there is a handle leak in the program is frequently called fopen without fclose, the appearance of this situation is just the beginning of the first time can open successfully
After a period of time, how all open unsuccessful, check the path and permissions are no problem, then you should check whether the handle leaked. General Linux supports up to 1000 a dozen
Handle, open too much, then the other can not open the

4 by examining the errno to analyze the positioning problem, errno is a value of int, which is defined in errno.h and does not need to be defined by itself.
You can view error messages through Strerror (errno), errno is an important way to debug your program.

Note: errno is the last error code for recording the system.

For example  
FILE *fp = NULL;
if (FP =fopen ("./test/1.txt", "r") = = NULL)
{
printf ("open fail errno =%d reason =%s \ n", errno, Strerrno (errno) );
}
You need to point out that this is a problem with printf, so the precinct errno is the last error code to record the system, then you may not get the error code we want, but the best way to mislead
is
FILE *fp = NULL;
int errnum = 0;
if (FP =fopen ("./test/1.txt", "r") = = NULL)
{
Errnum = errno;
printf ("open fail errno =%d reason =%s \ n", Errnum, Strerrno (Errnum));
}

Common errno error codes are as follows:
#define EPERM 1/* Operation not permitted */ 
#define ENOENT 2/* No such File or directory */
#define ESRCH 3/No Such process */
#define EIO 5 * I/O error */
#define ENXIO 6 * No such device or address */
#define E2BIG 7/Argument list too long */
#define ENOEXEC 8/* Exec format Error */
#define EBADF 9/* Bad file number */
#define ECHILD/* No child processes */


More please search online Cable, here on the analysis of these, follow-up found to continue to add ...

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.