读取 istream 的名称
Read the name of an istream
我有这样的东西:
istream ifs("/path/to/my/file.ppm", ios::binary);
所以现在,为了检查扩展文件,有必要获取文件的名称。 我正在使用自己的函数阅读:
... readPPM(std::istream& is) {}
可以从istream& 变量中获取字符串中的/path/to/my/file.ppm ?
你几乎可以肯定实际使用了
std::ifstream ifs(...);
// ^
但是,即便如此,流也不会保留用于打开它的名称:很少需要这样做,对于大多数应用程序来说,这将是一种浪费资源。也就是说,如果您以后需要该名称,则需要保留它。此外,并非所有流都有名称。例如,std::istringstream
没有名称。
如果无法将流的名称与流分开传递,则可以附加名称,例如,使用pword()
成员:
int name_index() {
static int rc = std::ios_base::xalloc(); // get an index to be used for the name
return rc;
}
// ...
std::string name("/path/to/my/file.ppm");
std::ifstream ifs(name, ios::binary);
ifs.pword(name_index()) = const_cast<char*>(name.c_str());
// ...
char const* stream_name = static_cast<char*>(ifs.pword(name_index()));
流不会以任何形状或形式维护指针,即,使用上述设置,name
需要比ifs
对象存活。如有必要,可以使用各种回调来维护与pword()
一起存储的对象,但这样做并不简单。
相关文章:
- 使用 istream 参数读取的 istream 函数
- 为什么 Clang std::ostream 写一个 std::istream 无法读取的双精度?
- 我可以从具有不同数字分隔符的istream中读取双值吗
- 读取 c++ istream 直到 EOF,同时丢弃内容
- 限制整数的 istream 读取宽度
- 是否可以覆盖 std::istream 读取方法?
- 从输入文件中读取c ,具有iStream Operator Overloading
- 从 istream 读取 bitset<0> 总是失败?
- IStream COM 读取到缓冲区,然后通过 winsock 发送
- 创建要从现有 istream 读取的子流
- 如何从 c++ std::istream 读取固定数量的字节
- 如何从IStream读取一个char数组并将其写入另一个IStream
- 故障后的istream读取
- 从 std::istream 读取时,有没有比 reinterpret_cast<char*>更好的选择?
- 使用派生类"istream"读取基类的属性
- 我可以用c++中的istream读取二进制数据吗
- 从istream读取自定义类型失败时的预期行为
- 用istream读取boost basic_stream时出现问题
- 从IStream读取到缓冲区(音频)
- 如何手动将数据放入boost::asio::streambuf中,以便稍后使用std::istream读取