Getting filename (or path) from fstream

Error processing SSI file

Answers

  1. Maxton

    • 2015/2/12

    you may also design a little class which inherits from fstream and behaves like a fstream but also stores its file name.

  2. Palmieri

    • 2015/12/14

    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.

  3. Juan

    • 2020/9/18

    The fstream class doesn't store the filename, and doesn't provide any function for retrieving it. 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 //..

  4. Emir

    • 2019/6/4

    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.

  5. Mohammed

    • 2016/10/7

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

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

    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.

  6. Bobby

    • 2016/2/2

    This is OS dependent but all the major systems behave more or less the same AFAIK. Windows example: // File structure: c:\folder\myprogram.exe c:\myfile.txt // Calling command from folder c:\folder > myprogram.exe. In the above example you could access myfile.txt with "c:/myfile.txt" or "../myfile.txt".

  7. Noah

    • 2021/5/30

    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. Asked By: 

  8. Richard

    • 2020/4/6

    Not likely to keep the path or filename because it is only needed to pass along to the operating system so that it can open the file and return the handle to fstream. You could subclass fstream so that it saves that information. This is for work, so I'd rather not create more files in the baseline.

  9. Vivaan

    • 2017/12/22

    Can I get a file name or its path from a fstream object? I looked through the methods anything close to it.

  10. Harlan

    • 2016/6/21

    On Mon, Feb 03, 2003 at 10:56:30PM +0530, Ajay Bansal wrote: > I have a fstream object for which I need the file name & path of the > file. i.e. file name which was used to create the fstream object.

  11. Hudson

    • 2021/7/10

    I'm passing an ofstream object to a function within my program. In that function, I'd like to extract the path and filename of the ofstream 

  12. D'Amico

    • 2020/1/2

    c++ getting the filename and path of an ofstream object. zmohamed. 1. I'm passing an ofstream object to a function within my program. In that function, I'd like to extract the path and filename of the ofstream object, modify the filename only, create a new ofstream object using the old path and new filename and write something to it.

  13. Finn

    • 2021/10/9

    1. When I use this main parameter to get drag&drop .txt file, the ofstream doesn't work like before It won't export if 

  14. Sylas

    • 2018/10/5

    In C++17 you can use the filesystem library and hard code the path, if this is what you really want to do. #include <filesystem> #include <fstream> namespace fs=std::filesystem; int main() { fs::path p = "your-absolute-path-goes-here"; // if(fs::exists(p)) { std::ofstream file(p); file << "Hello world!"

  15. Ezekiel

    • 2016/4/6

    inherits from istream, and the class ofstream (output file stream) inherits In both ways of opening a file, you can specify the file path or file name 

  16. Elliot

    • 2018/4/25

    // Example program #include <iostream> #include <string> #include <fstream> void GetFileInfo(const std::string& Filename) { std::fstream File(Filename); // do stuff with File} int main() { GetFileInfo("C:\\foldername\\filename.txt"); }

  17. Colter

    • 2019/8/23

    Get FileName using Boost & C++17 FileSystem Library · getFileName · { · // Create a Path object from File Path · pathObj · // Check if file name is 

  18. Marshall

    • 2015/5/2

    If it is missing (and the first element other than the root name is a file name), then the path is relative and requires another path as the 

  19. Stewart

    • 2016/4/2

    Gets the absolute path of the file opened in the FileStream. FStream.Name); tempState.FStream.Close(); // Signal the main thread that the verification 

Comments are closed.

More Posts