How to use fstream objects with relative path?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 18:37:11

问题


Do I always have to specify absolute path for objects instantiated from std::fstream class? In other words, is there a way to specify just relative path to them such as project path?


回答1:


You can use relative paths as well. But they are relative to the environment you call your executable from.

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". If myprogram.exe was called from the root c:\ only the absolute path would work, but instead "myfile.txt" would work.

As Rob Kennedy said in the comments there's really nothing special about paths regarding fstream. But here is a code example using a relative path:

#include <fstream>
int main() {
    std::ifstream ifs("../myfile.txt");
    ... // Do something sensible with the file
}



回答2:


If you have an .exe file running from C:\Users\Me and you want to write a file to C:\Users\Me\You\text.txt, then all what you need to do is to add the current path operator ., so:

std::ifstream ifs(".\\you\\myfile.txt");

will work




回答3:


You can use relative paths. They're treated the same as relative paths for any other file operations, like fopen; there's nothing special about fstream in that regard.

Exactly how they're treated is implementation-defined; they'll usually be interpretted relative to your process's current working directory, which is not necessarily the same as the directory your program's executable file lives in. Some operating systems might also provide a single working directory shared by all threads, so you might get unexpected results if a thread changes the working directory at the same time another thread tries to use a relative path.




回答4:


The behaviour is OS specific. Therefore, the best way to handle this IMHO is to make it somebody else's problem. Read the path to the file to open as a string from the user (e.g: command line argument, config file, env variable etc..) then pass that string directly to the constructor of fstream. Document that this is how your program behaves.

I wrote more about path manipulation here: https://stackoverflow.com/a/40980510/2345997




回答5:


Say you have a src folder directly under your project directory and the src folder contains another tmp_folder folder which contains a txt file named readMe.txt. So the txt file can be read in this way

std::ifstream fin("../src/tmp_folder/readMe.txt");



回答6:


You can specify a path relative to current directory. On Windows you may call GetCurrentDirectory to retrieve current directory or call SetCurrentDirectory to set current directory. There are also some CRT functions available.




回答7:


On linux also:

// main.cpp
int main() {
    ifstream myFile("../Folder/readme.txt");
    // ...
}

Assuming the folder structure is something like this:

/usr/Douments/dev/MyProject/main.cpp /usr/Documents/dev/MyProject/Folder/readme.txt



来源:https://stackoverflow.com/questions/8068921/how-to-use-fstream-objects-with-relative-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!