简体   繁体   English

如何检查文件指针是否有效?

[英]How do I check if a file pointer is valid?

I am using fseek and fread on a FILE * like this:我在 FILE * 上使用 fseek 和 fread,如下所示:

void CAudioDataItem::finalizeItem_ReadEncodedBytesWithoutDecoding(FILE* uFile, CLog* uLog, double& uTimeReadEncodedBytes)
{
    //t.start();
    fseek(uFile, iFirstByteToRead, SEEK_SET);
    fread(&m_EncodedBytes[0], sizeof(unsigned char), m_lLenEncodedBytes, uFile);
    //t.stop();

However, it doesn't work, the bytes are all 0.然而,它不起作用,字节都为0。

I suspect that somewhere in my app, the FILE pointer gets invalid or something else becomes wrong with this pointer.我怀疑在我的应用程序中的某个地方,FILE 指针变得无效或者该指针出现其他问题。

How could I check if this file handle is still valid and "usable"?我如何检查此文件句柄是否仍然有效且“可用”?

Here is a screenshot:这是一个截图:

在此处输入图像描述


Here is a screenshot of a situation where it still works.这是它仍然有效的情况的屏幕截图。 As one can see, the _Placeholder is different.可以看出, _Placeholder是不同的。 What does this _Placeholder mean?这个_Placeholder是什么意思?

在此处输入图像描述

If the question is on C++ as one of the tags imply,如果问题是关于 C++ 的,如其中一个标签所暗示的那样,

There's typically no way to tell if the pointer dangles , ie, points to memory that no longer holds the object the pointer is supposed to point to.通常没有办法判断指针是否悬垂,即指向 memory 不再包含指针应该指向的 object。

from Effective Modern C++ , in the introduction to Chapter 4 - Smart Pointers , page 117.来自Effective Modern C++ ,在第 4 章 - 智能指针的介绍中,第 117 页。

If it's on C, then from the comments we know the same point holds for C.如果它在 C 上,那么从评论中我们知道同样的观点适用于 C。

As regards _Placeholder=0x00000000 when it doesn't work vs _Placeholder=0x1bc715bf when it works, I'm not sure why the numbers (which are memory addresses, I believe) are those that you see;关于_Placeholder=0x00000000当它不起作用时与_Placeholder=0x1bc715bf当它起作用时,我不确定为什么数字(我相信是 memory 地址)是你看到的那些; maybe _Placeholder=0x00000000 means that something in the program has set the pointer to nullptr , but the point is that once that (or something else which invalidates the pointer) has happened, you can't do anything to query it.也许_Placeholder=0x00000000意味着程序中的某些内容已将指针设置为nullptr ,但关键是一旦发生这种情况(或其他使指针无效的事情),您将无法执行任何操作来查询它。

The only way to go about this is to surrender to the evidence that the program has a bug that results in undefined behavior and try to debug it. go 关于此的唯一方法是接受程序存在导致未定义行为的错误的证据并尝试调试它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM