Backslash or Forward Slash in Qt? - c++

I'm trying to open a file in Qt.I used double back slash in this function
doc->dynamicCall("Open(QVariant)", "E:\\QT\\build-untitled-Desktop_Qt_5_7_0_MSVC2015_64bit-Debug\\My Question.doc")
and it works. However, my directory variable's absolute path returns this, which has forward slash:
"E:/QT/build-untitled-Desktop_Qt_5_7_0_MSVC2015_64bit-Debug/My Question.doc"
and It doesn't work. Error says: " Sorry, we couldn't find your file. Is it possible it was moved, renamed or deleted?" I tried everything and it only works with double back slash.
I do know that I have to escape \ as \\, but how do I write this function using my variable ?

I think toNativeSeparators function may help you. Since you are on Windows it will replace forward slashes into backward slashes.
Code is like that:
string path = "E:/QT/build-untitled-Desktop_Qt_5_7_0_MSVC2015_64bit-Debug/My Question.doc";
doc->dynamicCall("Open(QVariant)", QDir::toNativeSeparators(path));

Related

Need regex code to get the file path without the file name and extension

I need to get the file path using regex without obtaining the file name and its extension.
Sample:
c:\testfolder1\testfolder2\abc.pdf
Expected output:
c:\testfolder1\testfolder2\
i tried the regex which obtain the filepath with extension but i need the remaining string of it without having filename and its extension.
i need the opposite of the below regex.
[ \w-]*.pdf
which obtain abc.pdf but instead i need output as c:\testfolder1\testfolder2
Sample:
c:\testfolder1\testfolder2\abc.pdf
Expected output:
c:\testfolder1\testfolder2\
This would work:
.*\\
It finds everything before the backslash.
RegExp probably isn't what you want to use for this, but here you go:
.*[\\/]
It should also handle forward slashes in case you have to deal with MacOS or Linux paths.

Convert path to \\

Okay, after two days of searching the web and MSDN, I didn't found any real solution to this problem, so I'm gonna ask here in hope I've overlooked something.
I have open dialog window, and after I get location from selected file, it gives the string in following way C:\file.exe. For next part of mine program I need C:\\file.exe. Is there any Microsoft function that can solve this problem, or some workaround?
ofn.lpstrFile = fileName;
char fileNameStr[sizeof(fileName)+1] = "";
if (GetOpenFileName(&ofn))
strcpy(fileNameStr, fileName);
DeleteFile(fileName); // doesn't works, invalid path
I've posted only this part of code, because everything else works fine and isn't relevant to this problem. Any assistence is greatly appreciated, as I'm going mad in last two days.
You are confusing the requirement in C and C++ to escape backslash characters in string literals with what Windows requires.
Windows allows double backslashes in paths in only two circumstances:
Paths that begin with "\\?\"
Paths that refer to share names such as "\\myserver\foo"
Therefore, "C:\\file.exe" is never a valid path.
The problem here is that Microsoft made the (disastrous) decision decades ago to use backslashes as path separators rather than forward slashes like UNIX uses. That decision has been haunting Windows programmers since the early 1980s because C and C++ use the backslash as an escape character in string literals (and only in literals).
So in C or C++ if you type something like DeleteFile("c:\file.exe") what DeleteFile will see is "c:ile.exe" with an unprintable 0xf inserted between the colon and "ile.exe". That's because the compiler sees the backslash and interprets it to mean the next character isn't what it appears to be. In this case, the next character is an f, which is a valid hex digit. Therefore, the compiler converts "\f" into the character 0xf, which isn't valid in a file name.
So how do you create the path "c:\file.exe" in a C/C++ program? You have two choices:
"c:/file.exe"
"c:\\file.exe"
The first choice works because in the Win32 API (and only the API, not the command line), forward slashes in paths are accepted as path separators. The second choice works because the first backslash tells the compiler to treat the next character specially. If the next character is a hex digit, that's what you will get. If the next character is another backslash, it will be interpreted as exactly that and your string will be correct.
The library Boost.Filesystem "provides portable facilities to query and manipulate paths, files, and directories".
In short, you should not use strings as file or path names. Use boost::filesystem::path instead. You can still init it from a string or char* and you can convert it back to std::string, but all manipulations and decorations will be done correctly by the class.
Im guessing you mean convert "C:\file.exe" to "C:\\file.exe"
std::string output_string;
for (auto character : input_string)
{
if (character == '\\')
{
output_string.push_back(character);
}
output_string.push_back(character);
}
Please note it is actually looking for a single backslash to replace, the double backslash used in the code is to escape the first one.

Boost: Why write_json changing content

I am trying to read and save a json file. The problem is that when I call write_json(pt, "newFile.json") it is changing the content of some fields like:
input:
"field1":"path/to/file.txt"
is changed to:
"field1":"path\/to\/file.txt"
Is this a bug? How to fix it?
It means it escapes the forward slash. The JSON spec says you can escape forward slash, but it's not mandatory. As for the reason, here is a good explanation.

How do I open a file within notepad++ with C++?

I'm trying to get my c++ program to open an sql file in notepad++. I can get it to open with notepad like this:
system("notepad.exe script_foo.sql");
But that's undesirable as it's not formatted. When I try to substitute notepad.exe for notepad++.exe like this:
system("'C:\Program Files\Notepad++\notepad++.exe' script_foo.sql");
I get a invalid syntax error.
Any issues where I'm going wrong?
The WinNT shell uses double-quotes to include spaces in a file name. Single quotes are not recognized. So you need
"C:\Program Files\Notepad++\notepad++.exe" script_foo.sql
as your command.
To embed this in C++ source code, you'll need to escape backslashes (as Andre already mentioned) and also the double-quotes.
system("\"C:\\Program Files\\Notepad++\\notepad++.exe\" script_foo.sql");
In C++, the backslash character \ is an escape character in strings. You need to double the backslashes to achieve what you really want:
system("'C:\\Program Files\\Notepad++\\notepad++.exe' script_foo.sql");

Unable to open fstream when specifying an absolute path

I know this is rather laughable, but I can't seem to get simple C++ ofstream code to work. Can you please tell me what could possibly be wrong with the following code:
#include <fstream>
...
ofstream File("C:\temp.txt");
if(File)
File << "lolwtf";
Opening the ofstream fails whenever I specify an absolute path. Relative paths seems to work with no issues. I'm really uncertain as to what the issue is here.
Your path is invalid:
"C:\temp.txt"
The \ is escaping the "t" as a horizontal tab character, so the path value ends up as:
"C: emp.txt"
What you want is:
"C:\\temp.txt"
or
"C:/temp.txt"
Even though Windows people seem to prefer the non-standard '\' character as a path separator, the standard '/' works perfectly and avoids annoying problems like this.
So, my advice is to stick to forward slashes...
std::ofstream File("C:/temp.txt");
The problem is in your string, you are not escaping the backslash.
ofstream File("C:\\temp.txt");