C++ 如何使用具有相对路径的 fstream 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8068921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use fstream objects with relative path?
提问by user336359
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?
我是否总是必须为从std::fstream
类实例化的对象指定绝对路径?换句话说,有没有办法只指定它们的相对路径,例如项目路径?
采纳答案by Kleist
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.
这取决于操作系统,但所有主要系统的行为或多或少都与 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.
在上面的示例中,您可以使用“c:/myfile.txt”或“../myfile.txt”访问 myfile.txt。如果从根调用 myprogram.exe,则c:\
只有绝对路径可以工作,而“myfile.txt”可以工作。
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:
正如 Rob Kennedy 在评论中所说,关于 fstream 的路径真的没有什么特别之处。但这是一个使用相对路径的代码示例:
#include <fstream>
int main() {
std::ifstream ifs("../myfile.txt");
... // Do something sensible with the file
}
回答by Samer
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:
如果您有一个.exe
正在运行的文件C:\Users\Me
并且您想将一个文件写入C:\Users\Me\You\text.txt
,那么您需要做的就是添加当前路径 operator .
,因此:
std::ifstream ifs(".\you\myfile.txt");
will work
将工作
回答by Rob Kennedy
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.
您可以使用相对路径。它们被视为与任何其他文件操作的相对路径相同,例如fopen
;fstream
在这方面没有什么特别之处。
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 allthreads, 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.
它们的确切处理方式是实现定义的;它们通常会相对于您的进程的当前工作目录进行解释,该目录不一定与您程序的可执行文件所在的目录相同。某些操作系统可能还提供所有线程共享的单个工作目录,因此您可能会遇到意外如果一个线程在另一个线程尝试使用相对路径的同时更改工作目录,则会产生结果。
回答by user1976
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.
该行为是特定于操作系统的。因此,处理这个恕我直言的最好方法是让它成为别人的问题。从用户读取要作为字符串打开的文件路径(例如:命令行参数、配置文件、环境变量等),然后将该字符串直接传递给 fstream 的构造函数。记录这就是您的程序的行为方式。
I wrote more about path manipulation here: https://stackoverflow.com/a/40980510/2345997
我在这里写了更多关于路径操作的内容:https: //stackoverflow.com/a/40980510/2345997
回答by Martin
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
假设您src
的项目目录下有一个文件夹,该src
文件夹包含另一个tmp_folder
文件夹,其中包含一个名为 .txt 的 txt 文件readMe.txt
。所以txt文件可以这样读取
std::ifstream fin("../src/tmp_folder/readMe.txt");
回答by A-Sharabiani
On linux also:
在 linux 上也有:
// 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
/usr/Douments/dev/MyProject/main.cpp
/usr/Documents/dev/MyProject/Folder/readme.txt
回答by 0902horn
You can specify a path relative to current directory. On Windows you may call GetCurrentDirectoryto retrieve current directoryor call SetCurrentDirectoryto set current directory. There are also some CRT functions available.
您可以指定相对于当前目录的路径。在 Windows 上,您可以调用GetCurrentDirectory来检索当前目录或调用SetCurrentDirectory来设置当前目录。还有一些 CRT 功能可用。