Obtener el nombre de archivo (o ruta) de fstream


¿Puedo obtener un nombre de archivo o su ruta de acceso desde un objeto fstream? Miré a través de los métodos de fstream y no encontré nada cercano a él.

Author: Nan Xiao, 2012-05-27

2 answers

No, eso no es posible, al menos en la implementación conforme al Estándar de la biblioteca.

La clase fstream no almacena el nombre del archivo, y no proporciona ninguna función para recuperarla.

Así que una forma de hacer un seguimiento de esta información es usar std::map como:

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
    //...
}

O simplemente pasa el nombre del archivo también.

 37
Author: Nawaz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-27 11:10:57

También puede diseñar una pequeña clase que hereda de fstream y se comporta como un fstream pero también almacena su nombre de archivo.

 20
Author: Walter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-27 11:34:09