fopen 返回 NULL,但 perror 打印成功?

发布于 2024-11-07 01:16:02 字数 769 浏览 0 评论 0 原文

是的,我有这个代码:

if(argc>1){
           FILE * pFile = fopen(argv[1],"rb");
           perror("");
}else{
      FILE * pFile = fopen("hardcoded","rb");
}
if(pFile==NULL){
                puts("Unable to open source file");
                return -1;
}

但是,我得到了这个奇怪的输出:

Success
Unable to open source file

更奇怪的是,如果我这样做:

if(argc>1){
           FILE * pFile = fopen(argv[1],"rb");
           perror("");
}else{
      FILE * pFile = fopen("hardcoded","rb");
}
FILE * pFile = fopen("hardcoded","rb");
if(pFile==NULL){
                puts("Unable to open source file");
                return -1;
}

在硬编码存在的地方,一切都工作正常!

那些火苗是什么意思?

在 Ubuntu 上使用 GCC4 进行编译

Right, I've got this code:

if(argc>1){
           FILE * pFile = fopen(argv[1],"rb");
           perror("");
}else{
      FILE * pFile = fopen("hardcoded","rb");
}
if(pFile==NULL){
                puts("Unable to open source file");
                return -1;
}

However, I get this weird output:

Success
Unable to open source file

Weirdlier, if I do this:

if(argc>1){
           FILE * pFile = fopen(argv[1],"rb");
           perror("");
}else{
      FILE * pFile = fopen("hardcoded","rb");
}
FILE * pFile = fopen("hardcoded","rb");
if(pFile==NULL){
                puts("Unable to open source file");
                return -1;
}

Where hardcoded exists, it all works fine!

What the blazes does that mean?

Compiling with GCC4 on Ubuntu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

酒儿 2024-11-14 01:16:02

我对你的代码编译感到惊讶,因为你声明 FILE *pFile 范围为 if 和 else 块。如果您在此之前已声明它,请删除 if/else 块中赋值前面的 FILE* 文本。

I'm surprised your code compiles, since the you are declaring FILE *pFile scoped to the if and the else blocks. If you've declare it prior to that, then remove the FILE* text in front of the assignments in the if/else blocks.

¢好甜 2024-11-14 01:16:02

不要在 if 语句中定义 pFile,否则您将失去范围。

FILE * pFile;
if(argc>1){
           pFile = fopen(argv[1],"rb");
           perror("");
}

Don't define pFile within the if statement, you're losing scope.

FILE * pFile;
if(argc>1){
           pFile = fopen(argv[1],"rb");
           perror("");
}
桃气十足 2024-11-14 01:16:02

我敢打赌,您之前的代码中已经有一个 FILE * pfile; 您没有包含在内。如果它位于所有块之外,并且具有静态存储持续时间,则将其初始化为 NULL。

当您在内部块中有 FILE * pfile = fopen(... 时,这两个 pfile 是两个不同的东西。因此,发生了什么:

您正在定义pfile 在一个块中,然后很好地打开它,然后到达块的末尾,它就像块中的任何其他本地变量一样被丢弃,

您只剩下原始的 pfile 。 ,您从未打开或分配任何内容到,这很可能是 NULL。

在第二种情况下,您打开一个文件并丢弃它,然后您将在与测试 if 相同的范围内得到一个 FILE * pfile 语句,这就是您正在测试的语句,因此

您只需定义一次 pfile 就可以了,因为额外的定义要么会导致编译器错误,要么会给您提供单独的版本。获取所有文件* 超出您的内部块,并且始终使用相同的 pfile

I'd bet you've got a FILE * pfile; earlier in your code that you didn't include. If it's outside all blocks, and has static storage duration, it's initialized to NULL.

When you then have FILE * pfile = fopen(... in an inner block, the two pfiles are two different things. Therefore, what's happening:

You're defining pfile in a block, and opening it fine. Then you reach the end of the block, and it's being discarded like any other variable local in a block.

You're left with the original pfile, which you never opened or assigned anything to, and that's likely to be NULL.

In the second case, you're opening a file and discarding it, and then you have a FILE * pfile in the same scope as the testing if statement, and that's the one you're testing, so it's fine.

What you need to do is define pfile only once, since additional definitions will either cause compiler errors or give you separate versions. Take all the FILE * out of your inner blocks, and just use the same pfile all the time.

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