从文件对象到文件名 [英] from file object to file name

查看:77
本文介绍了从文件对象到文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以从分别为C和C ++创建的文件对象中获取的文件名(包括其路径)

I wonder if we can get the file name including its path from the file object that we have created for the file name in C and in C++ respectively

FILE *fp = fopen(filename, mode); // in C
ofstream out(filename); // in C++
ifstream in(filename);  // in C++

谢谢!

推荐答案

通常不能.该文件可能从未有过文件名,因为它可能是标准输入,输出或错误或套接字.该文件可能也已被删除;至少在Unix上,您仍然可以读取或写入已删除的文件,因为进程会保留对该文件的引用,因此在引用计数变为零之前,不会删除基础文件本身.一个文件的名称可能不止一个.您可以将多个硬链接链接到一个文件.

You can't, in general. The file may not ever have had a file name, as it may be standard input, output, or error, or a socket. The file may have also been deleted; on Unix at least, you can still read to or write from a file that has been deleted, as the process retains a reference to it so the underlying file itself is not deleted until the reference count goes to zero. There may also be more than one name for a file; you can have multiple hard links to a single file.

如果要保留有关文件来源的信息,建议您创建自己的由文件名和文件指针或流组成的结构或类.

If you want to retain the information about where a file came from, I would suggest creating your own struct or class that consists of a filename and the file pointer or stream.

这篇关于从文件对象到文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆