Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Win32 Console Application - system command 1

Status
Not open for further replies.

shetlandbob

Programmer
Mar 9, 2004
528
GB
Hi,

I think I'm having a bit og a mind blank, the following code doesn't do what I expect, any ideas why?
Code:
  sprintf ( text, "cd C:\\Program Files\\MyDir" ); 
  err = system ( "cd" );
  [b]err = system ( text );[/b]
  err = system ( "cd" );
  err = system ( "dir" );
When I run it in a console window I expected it to show the current directory, change to the "MyDir" directory, print the dir path and then list all files in the dir.

However the line in bold doesn't work, the console doesn't change dir? The dir does exist and the err value = 0.

Any ideas what I'm doing wrong?

Thanks

Robert Cumming
 
I expect every system call execute in its own context, ie then it starts it is your current directory, then you change it "locally" in the system call's context.

If you want to change you current dir use the _chdir function

MSDN said:
_chdir, _wchdir
Change the current working directory.

int _chdir( const char *dirname );

int _wchdir( const wchar_t *dirname );

/Per
[sub]
www.perfnurt.se[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top