Home >Backend Development >C#.Net Tutorial >What is the reason why fopen in C language fails to open a file?

What is the reason why fopen in C language fails to open a file?

烟雨青岚
烟雨青岚Original
2020-06-20 09:16:1510172browse

What is the reason why fopen in C language fails to open a file?

#What is the reason why fopen in c language fails to open a file?

The reason is: the program opens the file.txt file in the "r" mode, that is, the "read" mode, but the program does not specify where the file is, and the program cannot find it. to this file, so I thought it couldn't be opened.

1. Open the integrated development environment Dev-C and create a new source code file: main.c, as shown below:

What is the reason why fopen in C language fails to open a file?

On my computer, this source code is placed in the path "D:\CCode\Experience".

2. The source code of the program is as follows: If the file fails to be opened, the program will prompt "File opening failed".

What is the reason why fopen in C language fails to open a file?

#3. Let me compile it and it passes. As shown in the figure below:

What is the reason why fopen in C language fails to open a file?

4. Let me run it again. After a while, the running results are as follows:

What is the reason why fopen in C language fails to open a file?

From the running results, it prompts "File opening failed!". Why is this? There is nothing wrong with the program code!

5. In fact, the reason is that the program opens the file.txt file in the "r" mode, that is, the "read" mode, but the program does not specify where the file is, and the program cannot find the file. , so I thought it couldn’t be opened.

6. The solution is very simple, just create the file.txt file and specify the path of the file. Create a new file file.txt under the path "D:\CCode\Experience".

7. Compile and run again. The results are as follows and the output is normal.

What is the reason why fopen in C language fails to open a file?

Recommended tutorial: "C Language"

The above is the detailed content of What is the reason why fopen in C language fails to open a file?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn