调试时Fopen()无法打开文件?

发布于 2025-01-25 18:42:19 字数 992 浏览 5 评论 0原文

当我想使用fopen()读取调试中的文件时,fopen()始终返回null,并且在尝试后无法找到错误:

  1. 我只是运行代码,而fopen()效果很好,可以得到我想要的东西。 (但在调试中失败了)
  2. 我被证明文件(Hello.txt)存在
  3. 我编写一个简单的代码,例如:
#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;

  1. 我还尝试了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:

  1. I just run the code, and fopen() works well, getting what I want. (but failed in debugging)
  2. I am sured that the file (hello.txt) exists
  3. 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;

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南渊 2025-02-01 18:42:19
fp = fopen("F:\\notes\\assign\\bonus\\hello.txt","r"); //this line

fopen()(以及许多其他标准库功能)的故障将将errno设置为指示错误原因的错误代码。您可以通过添加这样的代码将其转换为正确的错误消息:

if ( fp == NULL )
{
    perror( "Failed to open hello.txt" );
    exit( 1 );
}

perror( ) 将把错误原因的描述附加到您作为参数的字符串中,并将其打印到stderr。如果要在其他地方记录错误消息, strerror()代码>将将错误消息写入字符串缓冲区。

fp = fopen("F:\\notes\\assign\\bonus\\hello.txt","r"); //this line

A failure by fopen() (and many other standard library functions) will set errno to an error code indicating the error cause. You can turn that into a proper error message by adding code like this:

if ( fp == NULL )
{
    perror( "Failed to open hello.txt" );
    exit( 1 );
}

perror() will append a description of the error cause to the string you have given as argument, and print that to stderr. If you want to log the error message elsewhere, strerror() will write the error message to a string buffer.

请你别敷衍 2025-02-01 18:42:19

您需要将工作区路径添加到启动。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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文