Qt: QProcess result doesn't match result at prompt - c++

I'm trying to execute the following command from Qt:
explorer /select,C:\Temp Folder\temp.wav
This should show the file temp.wav as selected in an Explorer window. It works correctly when run from the command prompt.
However, when I try the following in Qt:
QProcess::startDetached(
"explorer",
QStringList("/select,C:\\Temp Folder\\temp.wav")
);
it doesn't work -- it opens Explorer but puts me in the "My Documents" folder. If I rename the folder to one without a space (TempFolder), it works correctly.
I've tried escaping the space in the folder name, placing quotes around the entire path, and many other combinations without success. Many combinations work correctly in cmd but do not seem to work when called through QProcess::startDetached.
The most confusing part is that the code I'm trying to copy is from QtCreator source code where they use something similar to open up a file in the Explorer window. Theirs successfully opens files with spaces in the path, but I just can't seem to recreate it!

When you make such call:
QProcess::startDetached("explorer",
QStringList("/select,C:\\Program Files\\7-Zip\\7z.exe"));
Qt transforms the argument string into the:
explorer "/select,C:\Program Files\7-Zip\7z.exe"
which is not a valid option to open Explorer and select the given file. This happens, because your single argument has space(s) and Qt escapes it with quotes.
To fix this problem you need to make the following call:
QProcess::startDetached("explorer",
(QStringList() << "/select," << "C:\\Program Files\\7-zip\\7z.exe"));
i.e. pass two arguments. This will produce the following string:
explorer /select, "C:\Program Files\7-Zip\7z.exe"
which is valid and will do what is intended.

Related

How can I get the path of a file I am trying to open using file explorer's "open with" dialog box

So I have a very specific question that I realize is a bit stupid, however regardless of how I search for it, nothing really shows up.
I am aware that I can use system() to execute commands in cmd, and I am so done with writing
g++ <filename.cpp> & a.exe & del a.exe
every time I want to compile, build and then delete the exe of a c++ app. Therefore I plan on making a simple handling software using c++ to open other c++ apps. It will just take the name of the app I am trying to run and do all of the things said in the above code. My problem is, when using cmd I can write the name of the file I am running, however how can I get the name of the file if I double click?
Example would be to always open cpp files with the program I am making. Upon double click, it will open a cmd console, type the code for me and execute the program. Then when I close the program, the cmd will close. I am aware how to do all of that, except how to get the file name of the file I am opening using the program.
I have tried using different libraries and have not found anything similar or at least nothing that states of this use. I am not expecting the whole program nor do I want it, I realize it is a simple project, however I am struggling with this one part and am unable to find a solution.
I simply want to make my own starting script. Then in file explorer I right click on any .cpp file, click open with and the dialog box shows up. I click the program I have created and there I compile the .cpp file with the script of my choice, however I do not understand how to get the path from right click, open with, and using my own program. This is all withing file explorer, not within the software I am making.

g++ is not recognized as an internal or external command Windows 10

First off I would like to say I've seen the previous questions on this site, I've tried every solution but none fit my use case or solves my problem.
I am having trouble with the g++ complier being recognized, I've included this path:
C:\Program Files (x86)\mingw-w64\i686-7.2.0-posix-dwarf-rt_v5-rev1\mingw32\bin\g++.exe
which is where the current version of mingw is located (recently downloaded). I've also tried other options like changing the path to gcc.exe, and just regular bin. Someone please provide a detailed solution to this problem.
Other things i have tried and looked at closely would be:
http://stephencoakley.com/2015/01/21/guide-setting-up-a-simple-c-development-environment-on-windows
seeing as though I'm working through sublime text 3
Another thing Ive tried:
Ive tried to copy and paste the path into cmd and run it , but i find this error code:
C:\Users\Kxrk>C:\Program Files (x86)\mingw-w64\i686-7.2.0-posix-dwarf-rt_v5-rev1\mingw32\bin\g++.exe
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
So seeing that, i tried another way , and that is to drag the file and drop it into cmd and get this :
C:\Users\Kxrk>C:\Program Files (x86)\mingw-w64\i686-7.2.0-posix-dwarf-rt_v5-rev1\mingw32\bin\g++.exe
g++.exe: fatal error no input files
compilation terminated
when u drag and drop the file it has double quotes around it , so i tried editing the path to contain double quotes around it and the path automaticlly changes back after saving.
This was very simple , it was one of those weird cases.
To solve my problem what i did was:
1: uninstall , the current version of the mingw compiler , because i felt as though the one i had was corrupt in a way.
2:Redownloaded it the compiler from the website http://www.mingw.org/
3: set up the new Environmental variable where i save it , witch was C:\MinGW\bin
I had to install g++ from the command line(cmd ,command prompt)
by using this command mingw-get install g++witch is located inside bin on default
now i created one more directory in the environmental variables , C:\MinGW\bin\g++.exe
6.Now everything works , and is normal
If you are trying to run the compiler from the command line then you have to put double quotes around the path, because the path contains two whitespaces (this is the reason for the first error).
The reason for second error is that you didn't specify which C++ program you want to compile. You have to append the filename of your C++ input file to your command:
C:\Users\Kxrk>"C:\Program Files (x86)\mingw-w64\i686-7.2.0-posix-dwarf-rt_v5-rev1\mingw32\bin\g++.exe" program.cpp
See Barmak Shemiranis answer if don't want to enter the full path all the time. After that you can just use this:
C:\Users\Kxrk>g++ program.cpp
You have to use quotation marks around the path so that is treated as a single path:
c:\>"c:\program files\path\g++.exe"
A better way is to set the environment variables. Open Environment variables windows (in Windows 10 you can type in "environment variables" in search box) or right click on "Computer" in desktop, open "Advanced System Settings" and find the button for "Environment variables"
Go to your command propmpt, type set path, it will show list of directories, copy them,
Now type set path=<data you copied> and then add a semicolon and possible directory to g++ usually C:\MinGW\bin

File as Command Line arguments

I am new to C++ and I am learning with Visual Studio.
I want to make a small program that reads a text from the command line and opens the text. I know that my program has to start like this:
int main (int argc, char *argv[] ){
ifstream File( argv[1] )
But I am super confused on how to run it from the Visual Studio Command Prompt. I know there are a lot of questions like this but I still haven't found what I am looking for. I read that you have to go to properties, Debug and change the command line arguments, but what exactly do I need to put in there? And what should I type in the VS Command Prompt.
Thank you!
From menu find: Project->Properties. Or from the Solution Explorer tree right click on the project and select Properties.
Now, in the opened dialog left pane select: Configuration Properties->Debugging
Then in the right pane grid find the line titled: Command Arguments
Fill it with the input file name (I think you better put it there as a full path, if there is a space in the path use with double quotas. like this:
Without space:
filepathwithoutspace.txt
or with spaces:
"file path with spaces.txt"
Good luck.
For starters, your code should not start like that: before
passing argv[1] to std::ifstream::ifstream, you should
verify that there is an argument, and output an error message
otherwise. As it is, you could end up passing a null pointer to
the constructor of ifstream, which will result (normally) in
a program crash.
As to how to run it: where did you put the executable? If
you're in the Visual Studio Command Prompt window, and have
invoked cl, then by default, the executable should be in the
local directory. Just enter .\name, where name is the name
of your program. If you've actually compiled it from within the
IDE, then in the command window, you should use cd to navigate
to where the executable was generated (which you can find out
from your properties), and invoke it as above; or you can simply
specify the path completely:
c:\Users\me\whereeverIPutTheThing\name.
If you want to debug (using the debugger), you need to specify:
1) the name of the executable (but the default should be good),
2) the parameters to pass it (what you want to see in
argv[1]—don't forget the quotes if it has a space in
it), and 3) the directory where the executable should run. The
second and third are somewhat interdependant: you can, for
example, specify just the filename in 2, and the path where the
file is located in 3, or you can specify the complete path to
the filename in 2, and forget about 3. Or use a combination of
the two: in practice, I tend to do everything from the root
directory of the project, so I'd specify a path relative to this
directory, and then the path from my project file to this root
in 3. (The way we have things set up, this is ..\..\.., but
I think you'll find it somewhat shorter.)

How do I open a file in c++ (other than notepad)

I was wondering how to open a file other than notepad... Our prof gave us an example:
s = "notepad.exe test.txt";
system(s.c_str());
That will open a file type of "notepad.exe" and the file name of "test.txt"
Main Question:
Now, I was wondering if there was a way to open other type of files, such as Microsoft Excel, Microsoft Word, Visual Studio, or 7zip.
My attempt opened something in a new cmd.exe (because of the START keyword):
fileNeededtoBeOpened = "START \"New Microsoft Office Excel Worksheet.xlsx\"";
system(fileNeededtoBeOpened.c_str());
(This code is slightly different from my original, where I'm trying to open a file from a vector...) but all I really need to know is instead of "notepad.exe" or "START" is there a different command to open different file types that aren't .txt
Also, a side note, I was reading on the internet that it wasn't safe to use system() to open files, is this correct?
I found the answer by myself... for those who are curious, here an the answers:
To open a text file: system(notepad)
To open an excel file: system(start excel)
To open a word doc file: system(start winword)
To open a 7z file: system(start 7zFM)
To open a visual studio file: system(start devenv)
I think you're confused.
System executes a command as you would on the command line (type cmd into the run prompt under start menu to get that).
So, when you type notepad.exe test.txt it's saying:
Open the program notepad.exe which is on the system path (so the
command line can find it to execute that program), and pass the
parameter test.txt to it.
Notepad itself decides what to do with test.txt, in this case it opens it.
So, you can tell it to run any command (program/executable) and pass any parameters to it in reality. If excel's on your system path, you can probably just type excel.exe to open it from your system command. Otherwise, find the location excel is installed in, and refer to it with the whole path to excel.exe and it will work fine.
For example, on my computer, executing "C:\Program Files\Microsoft Office\Office12\EXCEL.EXE" would open excel from the command line. I can pass further parameters to it by having more information (like filenames) after the Excel.exe" portion, just as you did in your notepad example. Using your system command should have equivilent behavior when that line is executed.
If you are only targeting Windows systems you can use the ShellExecuteEx function (part of the Win32 API). You can just pass a filename to it and it will launch what ever program is registered to handle that file type (just as if you opened the file from windows explorer). Documentation is available on MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx
There is some examples on Launching Applications (ShellExecute, ShellExecuteEx, SHELLEXECUTEINFO) MSDN article and lots more elsewhere around the internet.
AS the other guys mentioned , the System function only executes a cmd command, .. notepad.exe is in the system's path by default so it works directly
but for example for me if I want to open a zip file on my desktop , I'd type something like
"C:\Program Files\7-Zip\7zFM.exe" Desktop\zipfile.zip
that's when I'm currently at the my user's directory [by default] , or
"C:\Program Files\7-Zip\7zFM.exe" C:\Users\JiMMaR\Desktop\zipfile.zip
[where JiMMaR is my user name on windows 7]
note that this certain command works only on windows , if you are using another OS this won't work as it is
try doing a
fileNeededtoBeOpened = "\"C:\Program Files\7-Zip\7zFM.exe\" C:\Users\YOUR_USER_NAME\Desktop\zipfile.zip";
and see if that executes or not
edit:
if you cannot escape the space , then try this one
fileNeededtoBeOpened = "C:\Program~1\7-Zip\7zFM.exe C:\Users\YOUR_USER_NAME\Desktop\zipfile.zip";
Ok, firstly - system - is a function that starts a separate process to your program. Much the same as in a command window when you type the command. The command lines you provide will be dependent on the applications you want to launch.
Now, I was wondering if there was a way to open other type of files,
such as Microsoft Excel, Microsoft Word, Visual Studio, or 7zip.
Yes I would be pretty shocked if there wasn't a command line parameter you could specify to load a document in these apps at start up. (Ok not shocked, but it is pretty standard)
Does this have anything to do with c++ - not really - you need to look at references for the applications you mention and see what the command lines parameters are for them. Then craft a string and system(...) to your hearts content.

Why can't I launch notepad++ from the command line anymore?

I'm teaching myself some Django tonight using the local dev package at instantdjango.com
I made it through half the tutorial, building a basic map with points on it, then closed out of the console for a bit. I just fired it back up and now when I try to use notepad++ to edit files, console tells me it doesn't recognize the command. What happened?
When I as working through the first half, I was able to type: "notepad++ filename.ext" and I'd get a text editor that would pop up.
Now when I type that, it doesn't recognize the command.
How do I get back the ability to use the text editor and how did I lose it?
In a windows terminal, you can launch notepad++ with the following:
start notepad++ <filename>
Note that the filename is optional.
Most likely the directory in which the Notepad++ executable resides is not in your system's PATH. For information about fixing this please see How to set the path in Windows 2000 / Windows XP.
If you are using gitbash or cygwin, you can create an alias
alias np='start notepad++'
And use
np myfile.txt
This is what I have done, in this way you dont have to type notepad++
Create np.bat file with this set of commands
#echo off
start "" "C:\Program Files (x86)\Notepad++\notepad++.exe" %*
place np.bat file in c:\Windows
open the command prompt and type np or np myfile.txt and enter.
One way is to make a change to this registry key:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Notepad++.exe]
You can download a zipped .reg file from Donn Felker that lets you open a file with just an n shortcut.
I edited the .reg before running it and to make sure the path to Notepad++ is correct (e.g. C:\Program Files (x86)) and I also changed the shortcut to n instead of n.
Then double click to add to your registry.