Changing directories from a C console program

O

oddstray

Hi,

I'm using WinXP. I wrote a short C utility in which I want to change
directories and run programs. When I use the system() method and pass
in the same command that works in a cmd.exe session, it claims "Unable
to locate the directory". This happens for directories with and
without spaces in their names. Can anyone tell me what's wrong?
Thanks.


B


Here's the code snippet that fails:

if( _chdir( "C:" ) != 0 )
printf( "Unable to locate the directory: %s\n", "C:" );
else
system( "dir *.*");

// WORKS THIS FAR

if( _wchdir( "cd Perl" ) != 0 )
printf( "Unable to locate the directory: %s\n", "Perl" );
else
system( "dir *.*");

if( _wchdir( "cd C:\\Perl" ) != 0 )
printf( "Unable to locate the directory: %s\n", "C:\\Perl" );
else
system( "dir *.*");

// 'UNABLE TO LOCATE' EITHER OF THESE ALTHOUGH THIS DIRECTORY DOES
EXIST
 
J

Jack Klein

Hi,

I'm using WinXP. I wrote a short C utility in which I want to change
directories and run programs. When I use the system() method and pass
in the same command that works in a cmd.exe session, it claims "Unable
to locate the directory". This happens for directories with and
without spaces in their names. Can anyone tell me what's wrong?
Thanks.

Not here, we can't. The standard C language has no support for
directories at all. Not all systems have them and they are
implemented in vastly different ways on some platforms that do.
Here's the code snippet that fails:

if( _chdir( "C:" ) != 0 )
printf( "Unable to locate the directory: %s\n", "C:" );
else
system( "dir *.*");

// WORKS THIS FAR

if( _wchdir( "cd Perl" ) != 0 )
printf( "Unable to locate the directory: %s\n", "Perl" );
else
system( "dir *.*");

if( _wchdir( "cd C:\\Perl" ) != 0 )
printf( "Unable to locate the directory: %s\n", "C:\\Perl" );
else
system( "dir *.*");

// 'UNABLE TO LOCATE' EITHER OF THESE ALTHOUGH THIS DIRECTORY DOES
EXIST

There are no functions named "_chdir" or "_wchdir" in the standard C
library, they are extensions provided by your compiler. You need to
ask about this in one of Microsoft's support groups in the
family, or a group like

It is a Windows issue, not a C language one.
 
K

Keith Thompson

I'm using WinXP. I wrote a short C utility in which I want to change
directories and run programs. When I use the system() method and pass
in the same command that works in a cmd.exe session, it claims "Unable
to locate the directory". This happens for directories with and
without spaces in their names. Can anyone tell me what's wrong?
Thanks.


B


Here's the code snippet that fails:

if( _chdir( "C:" ) != 0 )
printf( "Unable to locate the directory: %s\n", "C:" );
else
system( "dir *.*");

// WORKS THIS FAR

if( _wchdir( "cd Perl" ) != 0 )
printf( "Unable to locate the directory: %s\n", "Perl" );
else
system( "dir *.*");

if( _wchdir( "cd C:\\Perl" ) != 0 )
printf( "Unable to locate the directory: %s\n", "C:\\Perl" );
else
system( "dir *.*");

// 'UNABLE TO LOCATE' EITHER OF THESE ALTHOUGH THIS DIRECTORY DOES
EXIST

We don't know. The _chdir() and _wchdir() functions aren't defined in
standard C; neither are directories, for that matter. You might try a
Windows-specific newsgroup. (But first, you might ask yourself
whether _wchdir() expects a directory name or a command string.)
 
J

jacob navia

oddstray said:
Hi,

I'm using WinXP. I wrote a short C utility in which I want to change
directories and run programs. When I use the system() method and pass
in the same command that works in a cmd.exe session, it claims "Unable
to locate the directory". This happens for directories with and
without spaces in their names. Can anyone tell me what's wrong?
Thanks.


B


Here's the code snippet that fails:

if( _chdir( "C:" ) != 0 )
printf( "Unable to locate the directory: %s\n", "C:" );
else
system( "dir *.*");

// WORKS THIS FAR

if( _wchdir( "cd Perl" ) != 0 )
printf( "Unable to locate the directory: %s\n", "Perl" );
else
system( "dir *.*");

if( _wchdir( "cd C:\\Perl" ) != 0 )
printf( "Unable to locate the directory: %s\n", "C:\\Perl" );
else
system( "dir *.*");

// 'UNABLE TO LOCATE' EITHER OF THESE ALTHOUGH THIS DIRECTORY DOES
EXIST

wchdir expects a wide character set string, not a multi byte one.
Hence, it doesn't find any directory you pass it.

Rule:
Use the documentation when something doesn't work.
 
A

Alan Balmer

I'm using WinXP. I wrote a short C utility in which I want to change
directories and run programs. When I use the system() method and pass
in the same command that works in a cmd.exe session, it claims "Unable
to locate the directory". This happens for directories with and
without spaces in their names. Can anyone tell me what's wrong?
Thanks.

Off topic here. Try a Windows programming group. However, I note that
you have one method which works - why not use it?

{OT} I wouldn't expect the system calls to work. You spawn a shell,
change directory in that shell, then close the shell and return to the
program you spawned it from. {/OT]
 
K

Kenneth Brody

oddstray wrote:
[...]
if( _wchdir( "cd Perl" ) != 0 )
[...]

Do you really have a directory called "cd Perl"?

If so, then something else is wrong, and you'll need to ask help in a
Windows-related group.

If not, then I hope you see the problem now.
 

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

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top