Changing Directories

J

John

All:

I'm using a Borland c++ compiler (v5.5) on a WinXP box and I'm interested in
writing a routine that has the OS change to a specified directory and
execute an application that resides there.

I'm having trouble tracking down the proper way of doing this with c++.

Your assistance would be appreciated.


Thanks,

Steve
 
A

Alf P. Steinbach

* John:
I'm using a Borland c++ compiler (v5.5) on a WinXP box and I'm interested in
writing a routine that has the OS change to a specified directory and
execute an application that resides there.

I'm having trouble tracking down the proper way of doing this with c++.

Your assistance would be appreciated.

Off-topicality:
Except if someone can mention a platform-independent library that does
this for you (Boost can do part of the job, but it's not a good idea to
change your own process' current directory, what you need is the OS way
to set the current directory of the new process) it's off-topic here.

Try [comp.os.ms-windows.programmer.win32].

Also see the FAQ for some other suitable groups.
 
R

Robbie Hatley

John said:
All:

I'm using a Borland c++ compiler (v5.5) on a WinXP box and I'm interested in
writing a routine that has the OS change to a specified directory and
execute an application that resides there.

I'm having trouble tracking down the proper way of doing this with c++.

Your assistance would be appreciated.


Thanks,

Steve


CLI or GUI?

If GUI, read your Win32 API manual; that's waaaaaaaaaay off-topic here.

If CLI, the C standard library function "system" (in <stdlib.h> )
would work:

system("CD \\sprat\\blat");
system("galumph");

Also, many compilers come with built-in chdir(), so you can do this:

bool ErrorFlag = chdir("/sprat/blat");
if (!ErrorFlag)
system("galumph");
else
std::cout << "Oops, no such dir." << std::endl;



--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant
 
R

Richard Herring

Robbie Hatley said:
CLI or GUI?

If GUI, read your Win32 API manual; that's waaaaaaaaaay off-topic here.

If CLI, --
that's waaaaaaaaaay off-topic here too.

the C standard library function "system" (in <stdlib.h> )
would work:

system("CD \\sprat\\blat");
system("galumph");

On some platforms, that will launch two unrelated processes, issuing one
command in each.

Off-topic advice tends to be worth every penny you paid for it.
 
B

Billy N. Patton

Robbie said:
CLI or GUI?

If GUI, read your Win32 API manual; that's waaaaaaaaaay off-topic here.

If CLI, the C standard library function "system" (in <stdlib.h> )
would work:

system("CD \\sprat\\blat");
system("galumph");

On solaris and linux the system will spawn another process, do the cd,
then kill the process. your current pwd will still be the same.
To use system you would have to
system("cd /my_dir; do some work");
This unsures that all work is done in the spawned process.

THis is not just a c++ situation, it's any language, compiled or
interpreted.

ex:
in csh
is different from
`cd /my_dir`
echo pwd
cd /my_dir
echo pwd

both echo's will show different values.

In one reply they suggested using chdir. Do this!
Unless you use a fully qualified path name the user
can have an alias set that may render your system call useless.
It's always better to use library reference calls than using
system("something"); Calling command line function inside a compiled or
interpreted language has no garuntees if the function reacts the same
from user to user or if it even exists from node to node.
Also, many compilers come with built-in chdir(), so you can do this:

bool ErrorFlag = chdir("/sprat/blat");
if (!ErrorFlag)
system("galumph");
else
std::cout << "Oops, no such dir." << std::endl;




--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,966
Members
47,515
Latest member
Harvey7327

Latest Threads

Top