从fstream获取文件名(或路径) [英] Getting filename (or path) from fstream

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

问题描述

我可以从 fstream 对象获取文件名或其路径吗?我浏览了 fstream 的方法,没有找到任何接近的方法。

Can I get a file name or its path from a fstream object? I looked through the methods of fstream and didn't find anything close to it.

推荐答案

否,这是不可能的,至少在标准的库实现中是不可能的。

No, that is not possible, not at least in the Standard conformant implementation of the library.

fstream类不存储文件名,并且

The fstream class doesn't store the filename, and doesn't provide any function for retrieving it.

因此,跟踪此信息的一种方法是使用 std :: map as:

So one way to keep track of this information is to use std::map as:

std::map<std::fstream*, std::string> stream_file_table;

void f()
{
  //when you open a file, do this:
  std::fstream file("somefile.txt");

  stream_file_table[&file] = "somefile.txt"; //store the filename

  //..
  g(file);
}
void g(std::fstream & file)
{
    std::string filename = stream_file_table[&file]; //get the filename
    //...
}

或者,只需传递文件名。

Or, simply pass around the filename as well.

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

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