fopen Open File Failure reason analysis

Source: Internet
Author: User

Scenario Description:

Today, my colleague said, in the existing tool code, you cannot use fopen to open the file, and then walk through to assist. But in solving the problem, it has been assumed that the file path has a character escaping the problem, there is no thought to print out the current cannot get to the file handle error, whether the file path does not exist. So the first is to replace the/, the result is conceivable, there is no role. Then replace it with \ \ or not. There is no reason, always thought.

Go back and write the simplest test procedure alone, no problem. The question of Unicode encoding is also excluded. At this time began Baidu fopen failure reasons to get. At the beginning of the plan to use the GetLastError function, but not be able to determine whether it is dedicated to the Windows API, so continue Baidu following results:

fprintf (stderr, "%s \ n", Strerror (errno)); results have no effect

Here is a useful piece of information:

fopen internal low AH use the CreateFile function, so the fopen call fails, you can also use GetLastError to view the error code.


Solution:

To fail, call the GetLastError () function, get the value of DWORD to 32, then open the tool and select View error code: The process cannot access the file because another program is accessing the file.


Reflection: If it is a character escaping, it must be unable to open a nonexistent file, as long as the error code can be solved by obtaining the problem.


Cross-examine: can multiple programs open read-only at the same time?

Answer: Of course it is possible, for reading, is shared, for writing, is exclusive.

fopen Open File Failure reason analysis

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.