有什么方法可以识别流吗?

发布于 2024-12-11 04:11:00 字数 104 浏览 0 评论 0原文

C++中有没有办法识别文件流?它是什么并不重要,只要从同一个文件创建的两个流具有相同的“id”即可;任何能让我说从同一个文件创建的两个流等价(不等于)的东西。

Is there any way in C++ to identify a file stream? It doesn't really matter what it is, as long as two streams created from the same file have the same "id"; anything that would allow me to say that two streams created from the same file are equivalent (not equal).

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

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

发布评论

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

评论(3

心欲静而疯不止 2024-12-18 04:11:00

唯一不特定于操作系统的通用标识符是文件路径或设备名称本身,但 iostream 对象不提供对该信息的访问。

一种解决方案是对流对象进行子类化并提供您需要的功能。一个不令人满意的例子:

class id_fstream : public std::fstream
{
    public :
        id_fstream( const char * filename, 
                    ios_base::openmode mode = ios_base::in | ios_base::out ) : 
                  fstream( filename, mode ), m_filename( filename)
        { 
            // do nothing
        } ;

        const std::string& filename()
        {
            return m_filename ;
        }

    private :
        std::string m_filename ;
} ;

然后您可以编写如下代码:

if( id_fstreamA.filename() == id_fstreamB.filename() )
{
    ...
}

但是,如果使用不同的相对路径或绝对路径或通过别名打开一个文件,则它不起作用。您可以通过操作系统特定的调用获取当前工作目录,然后解析所使用的任何非绝对路径的完整路径来解决该问题。

The only common identifier that isn't perhaos OS specific would be the file-path or device name itself, but iostream objects do not provide access to that information.

One solution is to sub-class the stream object and provide the functionality you need there. An unsatisfactory eample:

class id_fstream : public std::fstream
{
    public :
        id_fstream( const char * filename, 
                    ios_base::openmode mode = ios_base::in | ios_base::out ) : 
                  fstream( filename, mode ), m_filename( filename)
        { 
            // do nothing
        } ;

        const std::string& filename()
        {
            return m_filename ;
        }

    private :
        std::string m_filename ;
} ;

Then you can write code such as:

if( id_fstreamA.filename() == id_fstreamB.filename() )
{
    ...
}

It does not work however if one file was opened with a different reletive or absolute path or via an alias. You might solve that problem by obtaining the current working directory via an OS specific call and therby resolving a complete path to any non-absolute path used.

递刀给你 2024-12-18 04:11:00

据我所知,没有内置的方法来比较两个文件流。您要么必须比较两个指针,这需要您跟踪相同的流(从问题的格式来看,这在您的情况下可能是不可能的),要么读取两个文件流中的数据并进行比较。

As far as I can tell, there's no built-in way to compare two file streams. You would either have to compare the two pointers, which would require you to keep track of streams which are the same (which might not be possible in your case, judging from the format of the question), or read the data in both file streams and compare it instead.

不再让梦枯萎 2024-12-18 04:11:00

我相信没有这样的办法。

假设程序员有足够的方法来自己跟踪不同的流,或者创建一个自定义结构,将例如文件路径作为字符串放入该结构或类似的内容中。例如,您可以有一个像这样的结构:

struct FileStream {
    char* FilePath;
    istream FileStream;
}

然后要查看两个 FileStream 是否在同一个文件上,您可以执行以下操作:

myStringCompare(fs1.FilePath, fs2.FilePath);

我希望这会有所帮助。

I believe there is no such way.

It is assumed that the programmer has enough ways to keep track of different streams himself or makes a custom struct to put e.g. the file path as a string inside that struct or something similar. e.g. you could have a struct like this:

struct FileStream {
    char* FilePath;
    istream FileStream;
}

And then to see if two FileStreams are on the same file, you could do something like:

myStringCompare(fs1.FilePath, fs2.FilePath);

I hope this helps.

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