How to open a "Open directory" GUI with C++ in ubuntu? - c++

I am writing a C++ program for pattern recognition in OpenCV. Prompting for a user input, when user enters the option for "Viewing the Image Database", I want a window showing the files in that directory. I tried a system call with
system("nautilus /home/sample/index.jpg");
but it says that it could not find the directory and asks to check spelling. I was wondering if it was because of executing the program from a different directory. Could anyone explain why this is happening? If so, how to go around it?

You have to use default set file manager, not nautilus or any other.
Try to look over here: Linux default applications, How to find the default file manager

If you want to open nautilus, put the full path (probably /usr/bin/nautilus but you can easily discover using the which command).
Some further notes:
Not all Linux systems have Nautilus (Mint, for example, has replaced Nautilus with nemo)
If you want to make interactive windows, then you need a proper graphics library. In this case, since you are programming in C++, I suggest you to use Qt which is available on all Linux desktop distributions.

Related

How do I create a OS-neutral run configuration in Eclipse?

I would like to have a single run configuration that I can use whether I'm testing on a Windows system or a Unix system.
Everything seems to be fine with the exception that in the run configurations dialog the field for C/C++ Application requires the extension of the application as well.
So in Windows, it is: Path/To/Application.exe
and on a Unix system, it is: Path/To/Application
If there was someway to automatically detect the OS and apply the appropriate extension, I can share the same *.launch file with everyone working on the project regardless of their operating system.
I've looked a little bit into using Variables to set the path and file name, but most of them require additional dialogs and user inputs. Wild cards don't seem to work here either.
Any ideas?
Thanks to Marc K. for pointing out the quick fix of simply adding ".exe" extensions to the unix executables. Though, as he mentions, this isn't a perfect fix to the problem.

How to get "Open With" programs with their full paths from the registry on Windows with C++?

I'm using Qt, and it has a really helpful QSettings class that allows to easily access the Windows registry:
QSettings s("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.html\\OpenWithList", QSettings::NativeFormat);
s.value("a").toString() returns "chrome.exe".
Chrome is not in my computer's PATH. Running chrome.exe in the terminal results in a not found error. So I can't just run it in my C++ app.
I also can't get the icon for it, as I need full path.
So how do I find this full path? (In case of Chrome it's C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe)
Don't mess with the registry directly -- FindExecutable is designed to produce exactly what you want/need.

File path of .exe File in windows 7

I am working in a C++ project. I need to obtain the path of a installed software. (Eg. skype.exe) Is there any way to find the path via C++ coding or via Widows command prompt
Depends what you are needing it for, and how generic you want it.
You can use GetEnvironmentVariable to get the PATH variable, and search these paths.
You can use the App Paths registry key, as Gabe says. See also...
Usually there are pretty clear application-specific ways to find the path via the registry. Either via the HKLM/Software key or Uninstall. Careful with localization and hard-coding application names...
If you are just trying to launch the app, ShellExecute doesn't need the full path, it works almost like the "run" dialog box in the start menu.
It's in the App Paths registry key. For skype.exe you would look in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\skype.exe
You can call the standard Win32 API RegQueryValue to read it.

I can't go to a directory with C++

I need to change working directory of my project, so that output files go to a certain folder, not where all the project files are.
I'm using
system("cd secretdir/");
system("ls");
However, what I get, is the list of files in current project directory, not the "secretdir" one.
I'm on Mac OS X 10.6/Qt Creator 4.7 64 bit
Thanks!
You have to change the current working directory
http://www.linuxquestions.org/questions/programming-9/how-to-change-current-working-directory-in-c-550031/
Also, you should consider saving your output files with full path names instead of changing the working directory.
Your current code will spawn a subshell that will change its current directory to ./secretdir, then proceed to exit() without doing anything else.
Only then will ls run in another subshell, whose current directory is, of course, completely independent of what you did during your previous call to system().
That's probably where your problem lies. Are you looking for the chdir() function?
chdir("secretdir");
// From now on, the current directory of the process is `./secretdir`.
system("ls"); // Will probably behave as expected.
edit See Falmarri's response as I glossed over the first sentence of your question.
You can also use chdir
the following is crufty
The first system spawns a new process that does the cd. The second system spawns a completely different process that doesn't know what happened previously.
One thing you could do is:
system("ls secretdir/");
I'd highly recommend checking out QDir, QFile, and QProcess objects in the QT Creator help or online documentation since you are using it. They have very detailed and easy to understand documentation and using the tools available to you in QT should be a primary reason for choosing that tool much of QT rivals boost in portability and usability in my limited experience.
Also there is a great community for QT related questions at QTForum worth bookmarking especially if QT Creator is your primary development environment.
Using system should be avoided as general rule of thumb it is inefficient and insecure in many cases.
EDIT: Sorry I too glossed over your first sentence and jumped to the code bits. You can modify the project settings via the Projects tab in QT Creator to add a Custom Process step to the build where you can specify a working directory and then do a copy command to wherever you would like your output to go. You also may be able to specify a build output option within your .pro file directly ... once again the help and documentation is your friend however.
The function on Mac OSX is chdir("./secretdir"), although since it's a POSIX API it actually works the same on many other platforms as well.
Using system() is not portable so try to avoid to use directly "cd" like that. My advice is to use Boost filesystem.
There is a Two-minutes Tutorial !
Do
system("cd secretdir/; ls");
Or better yet use boost's filesystem library. Maybe just opendir.

xcode's executable product for c++ project

I've completed a simple numbers-version of the game "Towers of Hanoi" using xcode's command line tool in C++. Being accustomed to PC compilers like borland's and visual-c, I've attempted to "share" the game with others via e-mail by simply attaching the executable product built by xcode. However, the recipients can't run the program as it shows up in a different format - usually machine code, it sounds like.
After a bit of extensive searching, I'm realizing the complexity of building projects within xcode's IDE and the variations on the build settings/ targets, etc.
Anyone know how to build a self-contained c++ executable to be universally run? I don't go outside the STL library for this game. I'd greatly appreciate any help.
thanks
OS X is based on Unix, which uses plain binary files (i.e. no filename extension) as executables. If they have a certain "executable permission," they can be double-clicked to be run as executables, or run from the command line. However, this permission can't be sent over email - it's metadata within the file system itself, and this makes sense from a security standpoint (you wouldn't want spammers sending you executable viruses over email right?). So when the recipient receives the binary, they'll need to run the following command line command on it, assuming "hanoi" is the name of the binary file:
chmod +x /path/to/hanoi
If you really want to package it as an instantly double-clickable application, you'll need to give it a native UI and package it as a .app, then put that .app (which is actually a folder with the .app extension) in an archive to distribute. Sorry if that's more work than you were hoping for. Hope this helps!
Sharing applications across dot releases of the same OS can be notoriously difficult on the Mac (at least, as far as personal experience goes).
In order to be able to share your application with the least amount of effort, you will need to figure out:
What project type is this? Are you using any resources like images etc?
What version of the OS your friends are using? If they are not on the Mac, you're out of luck (or you'll have to recompile for their OS-es).
If they run Mac, check out that you have the same OS versions, if you have developed on Leopard and someone's running on SnowLeopard your application might simply fail. (I also ran into issues between Mac OS 10.5.4 and 10.5.3 so keep your fingers crossed.)
Check out what sort of hardware you are running. Are you building for your hardware (say, MacIntel) only or are you creating an Universal Binary?
Make sure that all resources are packaged into your application bundle. Make sure your application uses only relative paths.
Check if you are not writing to special folders (i.e. use only temp and/or word-writable locations, if you need to).
I wish I could give a more detailed/to the point reply but unfortunately you'll have to figure out some of the answers yourself (without any other specific information about the error you are getting).
If you're satisfied with a command line tool rather than a double-clickable app, it should suffice to zip it and attach that to the e-mail. Be sure to build universal if anyone you're sending to might be using a PowerPC-based Mac. Oh, and set the deployment target to the minimum OS that any recipient might be using.