Home > Article > Backend Development > 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:
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".
#3. Let me compile it and it passes. As shown in the figure below:
4. Let me run it again. After a while, the running results are as follows:
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.
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!