调试时Fopen()无法打开文件?
当我想使用fopen()读取调试中的文件时,fopen()始终返回null,并且在尝试后无法找到错误:
- 我只是运行代码,而fopen()效果很好,可以得到我想要的东西。 (但在调试中失败了)
- 我被证明文件(Hello.txt)存在
- 我编写一个简单的代码,例如:
#include<stdio.h>
int main()
{
FILE *fp;
char str[50];
fp = fopen("F:\\notes\\assign\\bonus\\hello.txt","r"); //this line
fgets(str, 50, fp);
printf("%s", str);
return 0;
}
此代码也不起作用。我在“这条线”上进行断点,然后观察文件 *fp的变化。
前: fp:0x00007ff6663b31110 {hello.exe!void(* pre_cpp_initializer)()}}} {_ placeholder = 0x00007ff6663ac74a4a4444444a4 {hello.exe
! fp:0x000001b7d6c3eb50 {_ placeholder = 0x000000000000000000}
> 您可以看到fopen()返回null;
- 我还尝试了freopen()和fopen_s(),但也失败了。
有关更多信息:
- 我使用VSCODE。我的编译器是“ clang” ,我的调试器是 Windows vs ,因此我必须在开发人员CMD提示中进行lauch vscode。
如果有人能帮助我,我会很感激。这打扰了我很长一段时间。
When I wanted to use fopen() to read a file in Debugging, fopen() always return NULL and I can't locate the error after trying:
- I just run the code, and fopen() works well, getting what I want. (but failed in debugging)
- I am sured that the file (hello.txt) exists
- I write a simple code like:
#include<stdio.h>
int main()
{
FILE *fp;
char str[50];
fp = fopen("F:\\notes\\assign\\bonus\\hello.txt","r"); //this line
fgets(str, 50, fp);
printf("%s", str);
return 0;
}
this code doesn't work too. I make a breakpoint at "this line" and watch how the FILE *fp changes.
before:fp: 0x00007ff663b31110 {hello.exe!void(* pre_cpp_initializer)()} {_Placeholder=0x00007ff663ac74a4 {hello.exe!pre_cpp_initialization(void)} }
after:fp: 0x000001b7d6c3eb50 {_Placeholder=0x0000000000000000 }
you can see fopen() returns NULL;
- I also tried freopen() and fopen_s(), but failed too.
For more information:
- I use vscode. my compiler is "clang", my debugger is Windows VS so that I have to lauch vscode in Developer Cmd Prompt.
I would appreciate if anyone can help me. This disturbs me for a long time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
fopen()
(以及许多其他标准库功能)的故障将将errno
设置为指示错误原因的错误代码。您可以通过添加这样的代码将其转换为正确的错误消息:perror( )
将把错误原因的描述附加到您作为参数的字符串中,并将其打印到stderr
。如果要在其他地方记录错误消息,strerror()代码>
将将错误消息写入字符串缓冲区。
A failure by
fopen()
(and many other standard library functions) will seterrno
to an error code indicating the error cause. You can turn that into a proper error message by adding code like this:perror()
will append a description of the error cause to the string you have given as argument, and print that tostderr
. If you want to log the error message elsewhere,strerror()
will write the error message to a string buffer.您需要将工作区路径添加到启动。JSON配置。
例如,Fallowing配置$ {WorksPaceFolder}/priv-256.pem”。
you need to add the workspace path to the launch.json configuration.
for example the fallowing configuration ${workspaceFolder}/priv-256.pem".