tags:

views:

164

answers:

5

For example: I'm on MS DOS, I have a source code in the folder C:\Documents and Settings\Programs. Can i make my source code use a program (for example gnuplot) that is in a random folder?

+2  A: 

http://www.codeproject.com/KB/system/newbiespawn.aspx

ShellExecute will look into the PATH environment variable, so you don't need to specify the full PATH. Now, if it's really a random location and it's not even in the PATH environment variable, then I guess you are out of luck.

If they aren't even in the PATH, then you have to search for it in the candidates folder. Here's sample code on how to traverse a file system path in C++.

And an example using Boost:

directoryList.h

#ifndef DIRECTORYLIST_H_INCLUDED
#define DIRECTORYLIST_H_INCLUDED
#define BOOST_FILESYSTEM_NO_DEPRECATED

#include <iostream>
#include <list>
#include <string>


class directoryList {

    public:
        directoryList();
        ~directoryList();
        std::list<std::string> getListing(std::string path);
};
#endif // DIRECTORYLIST_H_INCLUDED

directoryList.cpp

#include "boost/filesystem/operations.hpp"
#include "boost/filesystem/convenience.hpp"
#include "boost/filesystem/path.hpp"
#include "boost/progress.hpp"

#include "directoryList.h"

using namespace std;
namespace fs = boost::filesystem;

directoryList::directoryList() {}
directoryList::~directoryList() {}

list<string> directoryList::getListing(string base_dir) {

    list<string> rv;
    fs::path p(base_dir);

    for (fs::recursive_directory_iterator it(p); 
         it != fs::recursive_directory_iterator(); ++it) {

        string complete_filename = it->path().string();
        rv.insert(rv.begin(),complete_filename);

    }

    return rv;

}

Usage sample:

directoryList *dl = new directoryList();
filenames = dl->getListing("C:\\Program Files");
//search for the file here, or modify the getListing to supply a filter
Vinko Vrsalovic
MS-DOS (which is what the TS claims to have) doesn't have ShellExecute
MSalters
I doubt the OP has only DOS and not Windows. Just like I doubt that programs are installed on 'random' folders.
Vinko Vrsalovic
you're right i'm on windows..but gnuplot is not installed..i just have the .exe file..and what i meant was: how can i make my program find and run gnuplot even if i don't know in which folder it is??
Marco Orrù
thanx a lot really! I'm really starting right now with this C++ stuff and this blog is really useful to learn lots of new things!
Marco Orrù
A: 

Also there are some core functions _exec/exec and its modifications. Similar functions are available for Linux.

Marcin Gil
A: 

The location of source code has nothing to do with the way programs are located by system() call (I assume you use that call). The only relevant consideration is the location of the compiled executable.

Please take a look at PATH environment variable in Windows - this is how programs are found. It's a semicolon-separated list of directories where Windows looks for executables and BAT files and DLLs. To that list, current directory and (I think) the place where your EXE is are prepended.

The PATH is set in Windows XP from System control panel widget Advanced tab Environment button. For Vista, things are more complicated - you need to do it as administrator.

Arkadiy
A: 

As Vinko said, the PATH environment variable determines where Windows will look for program files.

It's usually best to avoid hard-coding the path of an executable into your compiled program. Even if gnuplot is in a particular folder on your machine then it might well not be in the same folder on someone else's computer. This would cause your call to the other program to fail. You could store it in the registry and let the user configure the program location, or provide an installer that searched for it.

A: 

Here are some options:

  1. Search in the system PATH for the executable you want to run
  2. Allow the user to specify the location on the command-line
  3. Store the location in a configuration file, and allow the user to specify it during install (if you have an install process) or by editing the file by hand

Ideally you'd do all 3

Anton
yeah i think i'll let the user specify the location..thank you!
Marco Orrù