Im running a long process using ProgressBox, and in that process im using System.Web.HttpContext.Current.Server.MapPath() method, which throws exception because System.Web.HttpContext.Current is null, Is there any method to do same functionality?
You can use
HostingEnvironment.MapPath(string path)
Yes.
Sitecore.IO.FileUtil.MapPath(string path)
Maps a virtual file path to a physical file path.
Parameters
path - A virtual file path.
Return Value
The physical file path.
Remarks
If the file path is blank, contains a backslash () or contains the string "://" the path itself is returned.
Example
The following example returns the physical filename of the mydata.xml file in the data folder. Afterwards the filename variable holds a value like "c:\inetpub\wwwroot\default website\sitecore\data\mydata.xml".
string filename = FileUtil.MapPath("/sitecore/data/mydata.xml");
Related
Well, the title say which is my problem
my .cpp code is:
GetPrivateProfileString("Server","MainDns","ServerDns",
g_DataBaseDns,sizeof(g_DataBaseDns),".\\Server.ini");
when I use:
GetPrivateProfileString("Server","MainDns","ServerDns",
g_DataBaseDns,sizeof(g_DataBaseDns),"..\\Server.ini");
and Put the ini file in the back folder: works normally!
What am I doing wrong?
You need to first call GetFullPathName() to obtain an absolute path, which is required according to the documentation for GetPrivateProfileString():
The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
For example:
TCHAR sAbsolutePathBuffer[2048] = _T("");
if (GetFullPathName(TEXT("Server.ini"), sizeof(sAbsolutePathBuffer)/sizeof(TCHAR), sAbsolutePathBuffer, NULL))
{
GetPrivateProfileString(TEXT("Server"), TEXT("MainDns"), TEXT("ServerDns"),
g_DataBaseDns, sizeof(g_DataBaseDns), sAbsolutePathBuffer);
}
I wish my app to write a file in a specified location, and therefore create the appropriate directory if needed.
The create dir operation isn't a problem for me, but I need the dir path.
I could extract if from the file path, but maybe is there a quick/concise/convenient way of doing the full operation?
I repeat, I'm not searching the basic makedir function, but one which would take the filename of a possibly non-existing file, or a simple qt function to extract the dir path string from the file path string, so I dont' have to write a func for such a basic task.
Use the following code:
const QString filePath = "C:/foo/bar/file.ini";
QDir().mkpath(QFileInfo(filePath).absolutePath());
This code will automatically create the path to the specified (nonexistent) file.
QFileInfo::absolutePath() extracts the absolute path to the specified file.
QDir::mkpath() creates the previously extracted path.
If you have a full path to the file and need to extract the folder path, you can do it this way:
QFile file(full_path_to_the_file);
QFileInfo info(file);
QString directoryPath = info.absolutePath();
//now you can check if the dir exists:
if(QDir(directoryPath).exists())
//do stuff
Depending on what exactly you need, you may prefer to use QFileInfo::canonicalPath() instead of absolutePath
Alternatively, you may also use QFileInfo::absoluteDir:
QFile file(full_path_to_the_file);
QFileInfo info(file);
if(info.absoluteDir().exists())
//do stuff
I'm coding with C++ and Qt.
I want to follow the symlink and get the absolute path with QFileInfo.
For example, /usr/local/extra is an symlink for /home/extra.
Then I need to convert /usr/local/extra/my_directory/ to /home/extra/my_directory.
I tried QFileInfo(path).canonicalPath() but it returns the parent directory only.
Use QFileInfo::canonicalFilePath() instead. canonicalPath() always returns the parent directory, while canonicalFilePath() actually includes the file (or directory) itself.
How about QFileInfo::symLinkTarget() ?
QString QFileInfo::symLinkTarget() const Returns the absolute path to
the file or directory a symlink (or shortcut on Windows) points to, or
a an empty string if the object isn't a symbolic link. This name may
not represent an existing file; it is only a string.
QFileInfo::exists() returns true if the symlink points to an existing
file. This function was introduced in Qt 4.2. See also exists(),
isSymLink(), isDir(), and isFile().
After I asked the question, I think I found the solution.
I should use QDir(path).canonicalPath() instead of QFileInfo(path).canonicalPath().
QString QDir::canonicalPath () const Returns the canonical path, i.e.
a path without symbolic links or redundant "." or ".." elements. On
systems that do not have symbolic links this function will always
return the same string that absolutePath() returns. If the canonical
path does not exist (normally due to dangling symbolic links)
canonicalPath() returns an empty string. Example:
// where /local/bin is a symlink to /usr/bin
QString bin = "/local/bin";
QDir binDir(bin);
QString canonicalBin = binDir.canonicalPath();
// canonicalBin now equals "/usr/bin"
QString ls = "/local/bin/ls"; // where ls is the executable "ls"
QDir lsDir(ls);
QString canonicalLs = lsDir.canonicalPath();
// canonicalLS now equals "/usr/bin/ls".
I successfully write to a file in the folder which run example:
// I run "test" executable file in "TestWrite File" folder
const char *path="/home/kingfisher/Desktop/TestWrite File/xml/kingfisher.txt";
std::ofstream file(path); //open in constructor
std::string data("data to write to file");
file << data;
However, If I try to write with dynamic path: *path = "/xml/kingfisher.txt", it goes wrong (in Windows, it will be fine)!! How I can write with dynamic path like above (not a specific path)? Thanks!
If by dynamic you mean relative, you need to get rid of the leading /, since that makes it an absolute path:
path = "xml/kingfisher.txt";
Just be aware that this file is relative to your current working directory so you will probably need to ensure that it is set to /home/kingfisher/Desktop/TestWrite File for this to work.
If, by dynamic, you mean changable, you can change it whenever you want:
const char *path = "/tmp/dummy";
:
path = "/home/.profile"; // Note path, NOT *path
The const simply means you're not permitted to change the data behind the pointer. You're able to change the pointer itself at will.
Not sure what you mean by "dynamic path"; a dynamic path is one that
will be read dynamically (and so will probably be in an std::string).
On the other hand, you seem to be confusing absolute path and relative
path. If the filename begins with a '/' (under Unix), or with a '/'
or a '\\', possibly preceded by "d:" under
Windows, it is absolute; the search for the file will start at the root
of the file system (on the specified drive in the case of Windows). In
all other cases, it is relative; the search for the file will start at
the current working directory. In your example, both
"/home/kingfisher/Desktop/TestWrite File/xml/kingfiger.txt" and
"/xml/kingfisher.txt" are absolute. If the current working directory
is "/home/kingfisher/Desktop/TestWrite File", then
"xml/kingfisher.txt" should find the file specified by the first
absolute pathname.
*path = "/xml/kingfisher.txt"
This is incorrect since it attempts to dereferences your const char* and modify the contents. This is undefined behaviour since the data is const.
Just declare your path to be a std::string to begin with:
std::string path = "/home/kingfisher/Desktop/TestWrite File/xml/kingfisher.txt";
Then later you can assign any other value you like to the std string and it's operator= will dynamically change it's internals for you:
path = "my/new/path";
You can use this with ofstream just as before and if you need to pass it to a function which expects a const char * just pass path.c_str().
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?
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
}
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
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.
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");
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
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.
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
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.