C++ - Remove last part of url - c++

Is there any way to remove part of URL ?
I have a path of file with specific extension , and I want remove file name and extension.
Here is my code:
QString path;
if (path.right(3) == "jpg")
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
else
?
for example :
I want
C:\Users\me\Desktop\
instead of
C:\Users\me\Desktop\file.exe

You can use the QFileInfo class and the absolutePath method:
QString filePath = QFileInfo(path).absolutePath();

Search backwards through the string for the first occurrence of '/' or '\' and stop.

Related

Setting the name of a text file to a variable in qt

I'm exporting data to a text file in qt every time a run a code. With my current code that file is overwritten each time. My question is how can I set the title to be a variable eg pulse_freq, this way new files will be created based on my variable values. I just can't get the syntax right.
Is there a way to put my files in a folder in the same directory as my build files? I need my code to be cross platform and if I use the full path name it's apparently incompatible with any non-windows OS. If I just name the files there'd be too much clutter in the folder. Relevant code is below:
// Export to data file
QString newname = QString::number(variables.nr_pulses);
QString filename = "C:/Users/BIC User/Documents/BIC Placement Documents/QT_data/Data.txt";
QFile file( filename );
You can just use something along lines:
QString s1 = "something";
QString s2 = " else";
QString s3 = s1 + s2;
QString concatenation with overloaded operator+ works like charm.
And about referencing folder you're in, instead of hardcoding its path, use QDir::currentPath()
Thus, your filename creation should look like the following:
QString folder = QDir::currentPath();
QString file = QString::number(variables.nr_pulses); //or whatever else you want it to be
QString extension = ".txt" // or whatever extension you want it to be
QString full_filename = folder + file + extension;
In order not to mess with appending string after the extension, just separate it into another QString and concatenate those 3 elements as above (folder + file + extension).

GetPath Remove and Concatenation Char

I will get environment 'APPDATA' and get back to previous directory from APPDATA\Roaming to APPDATA, then I want to concantenate with another directory APPDATA\Local\ finally replace '\' into '\'. Currently I had problem with it. How can I go back directory ?
Codes suppose to be
#include <string>
#include <iostream>
#include <algorithm>
char* path;
path= getenv("APPDATA"); <!--GetEnv APPDATA-->
??? <!-- Go back directory -->
strncpy(path,"Local\\stuff,12); <!-- add front directory-->
<!-- Replace slash to double slash -->
std::string s = path;
std::replace(s.begin(),s.end(), '\','\\');
Are you trying to remove the last directory in a string?
If so, you can use std::string::find_last_of( "\\" ) to find the last slash then use the return value to create a substring. The following example will do that.
std::string path = getenv("APPDATA"); //<!--GetEnv APPDATA-->
//??? <!-- Go back directory -->
std::size_t slashPosition = path.find_last_of( "\\" );
// Remove slash at the end if found easier to handle if trailing slash is/not found)
path = path.substr( 0, slashPosition );
path += "\\Local\\stuff"; //<!-- add front directory-->
I removed the code for replacing the single back with double since it would not work as written and I don't think it was necessary. I also used a std::string for the path variable to take advantage of methods in std::string.

How to get a complete path from one with wildcards?

I have a path like:
C:\path\to\my*file\
and I would like to get the corresponding full path (if it exists):
C:\path\to\my1file\
I tried with this Qt code, but the result is the same path I had at the beginning:
QStringList filters;
filters << "C:/path/to/my*file/";
QDir dir;
dir.setNameFilters(filters);
QStringList dirs = dir.entryList(filters);
_path = dirs.at(0); // get the first path only
Shouldn't I get all the files/directories that get through the filter?
Why is _path equal to "C:/path/to/my*file/"?
Is it possible to do the same thing with C++98/STL only? (In this project I cannot use Boost/C++11).
Use filters to filter files/folders, and set the path in QDir object:
QStringList filters;
filters << "my*file";
QDir dir("C:/path/to/");
QStringList dirs = dir.entryList(filters);
if (dirs.size() > 0)
{
qDebug() << dirs.at(0);
}
Expanding file names is called globbing. On Windows the functions FindFirstFile() / FindNextFile() do globbing.

Replace single quote in QFile file name

I want open a file which contains single quote but I can't open it.
File name example : QFile file("my'file.example")
I've tried with file.fileName().replace("\'", "\\\'") but it's the same result.
You are trying to replace "\'" but it is not on the original string so it will not work. Furthermore, QFile::filename return a copy of the filename property, and any modification (like replace) will be made on the copy. To play with the filename (before open), use
file.setFilename(file.fileName().myModificationOperation())
Have you tried with QFile file("my\'file.example")?
to test your parameter use the static call:
QString filename = "my\'file.example";
bool okay = QFile::exists(filename);

How to find whether a given path is absolute/relative and convert it to absolute for file manipulation?

I am writing a small windows script in javascript/jscript for finding a match for a regexp with a string that i got by manipulating a file.
The file path can be provided relative or absolute. How to find whether a given path is absolute/relative and convert it to absolute for file manipulation?
How to find whether a given path is absolute/relative ...
From the MSDN article Naming Files, Paths, and Namespaces:
A file name is relative to the current directory if it does not begin with one of the following:
A UNC name of any format, which always start with two backslash characters ("\\"). For more information, see the next section.
A disk designator with a backslash, for example "C:\" or "d:\".
A single backslash, for example, "\directory" or "\file.txt". This is also referred to as an absolute path.
So, strictly speaking, an absolute path is the one that begins with a single backslash (\). You can check this condition as follows:
if (/^\\(?!\\)/.test(path)) {
// path is absolute
}
else {
// path isn't absolute
}
But often by an absolute path we actually mean a fully qualified path. In this is the case, you need to check all three conditions in order to distinguish between full and relative paths. For example, your code could look like this:
function pathIsAbsolute(path)
{
if ( /^[A-Za-z]:\\/.test(path) ) return true;
if ( path.indexOf("\\") == 0 ) return true;
return false;
}
or (using a single regex and a bit less readable):
function pathIsAbsolute(path)
{
return /^(?:[A-Za-z]:)?\\/.test(path);
}
... and convert it to absolute for file manipulation?
Use the FileSystemObject.GetAbsolutePathName method:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var full_path = fso.GetAbsolutePathName(path);
To check whether the path is relative or absolute, look for a leading /.
If it doesn't have one, you need to concatenate the path to a base path. Some programming environments have a "current working directory", but Javascript that lives in the browser doesn't, so you just need to pick a base path and stick to it.
function full_path(my_path) {
var base_path = "/home/Sriram/htdocs/media";
var path_regex = /^\/.*$/;
if(path_regex.test(my_path)) {
return my_path;
} else {
return base_path + my_path;
}
}
Paths can contain newlines, which the javascript regex . won't match, so you might want to develop a more sophisticated regex to make sure all paths will work properly. However, I'd consider that outside the scope of this answer, and of my knowledge. :-)