473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Changing directories from a C console program

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
Nov 14 '05 #1
5 2094
On 26 Aug 2004 17:57:50 -0700, od******@gmail.com (oddstray) wrote in
comp.lang.c:
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
news:microsoft.public.vc.* family, or a group like
news:comp.os.ms-windows.programmer.win32.

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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
od******@gmail.com (oddstray) writes:
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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
oddstray wrote:
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.
Nov 14 '05 #4
On 26 Aug 2004 17:57:50 -0700, od******@gmail.com (oddstray) wrote:

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]

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #5
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.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: James Kimble | last post by:
I've inherited a program that was developed in Visual Studio and I'm having a problem with build times. The program is about 250,000 lines and I'm storing the entire project on a network drive....
5
by: skip | last post by:
A simple script like the one below lets me jump through a directory structure. However, if I run it from /this/directory and within it to go to /a/totally/different/directory... I'm still...
4
by: GujuBoy | last post by:
i have a ansi.py file that i use in LINUX to change the text color to STDOUT when i use the "print" function...but when i move this ansi.py file over the windows...it does not work is there a...
2
by: MENTAT | last post by:
Hi, I am trying to create an installer for my web application. So I added a web setup project to my solution (I am using VS.NET 2003). Been playing around with it since then and it basically...
5
by: eSolTec, Inc. 501(c)(3) | last post by:
Thank you in advance for any and all assistance. I'm looking for a way to programmatically scan all installed drives for programs that are installed. Can someone point me to code to look for say,...
9
by: silverburgh.meryl | last post by:
i am trying to use python to walk thru each subdirectory from a top directory. Here is my script: savedPagesDirectory = "/home/meryl/saved_pages/data" dir=open(savedPagesDirectory, 'r') ...
9
by: David | last post by:
With a non-server app there is one instance of the program running and one user 'using' it at a time. With this scenario I'm pretty comfortable with variable scope and lifetime. With a server app...
4
by: Edwin Velez | last post by:
http://msdn.microsoft.com/en-us/library/806sc8c5.aspx The URL above gives sample code for use within a Console Application. What I would like to do is use this code within a Windows Form. That...
11
by: =?Utf-8?B?UGF1bA==?= | last post by:
I did a google search but could find what I was looking for. I am creating a temporary folder and placing files in it with a web application. The temporary folder name needs to be changed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.