user336359
user336359

Reputation: 1284

How to use fstream objects with relative path?

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?

Upvotes: 52

Views: 149173

Answers (8)

JhWebDev
JhWebDev

Reputation: 402

What I ended up using was a relative path as identified on How to open a file with relative path in C++? which ended up being:

myFile.open("../Release/frequency.dat", ios::in);

*changing myFile to whatever your variable is.

Upvotes: 0

Samer
Samer

Reputation: 1980

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

Upvotes: 21

A-Sharabiani
A-Sharabiani

Reputation: 19339

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

Upvotes: 1

Martin
Martin

Reputation: 21

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");

Upvotes: 2

user1976
user1976

Reputation: 383

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

Upvotes: 1

0902horn
0902horn

Reputation: 188

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.

Upvotes: 0

Kleist
Kleist

Reputation: 7985

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
}

Upvotes: 39

Rob Kennedy
Rob Kennedy

Reputation: 163287

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.

Upvotes: 4

Related Questions