读书人

怎么调用system函数改变当前目录

发布时间: 2012-04-26 14:01:31 作者: rapoo

如何调用system函数改变当前目录
system("cd \");为什么不能将当前目录返回根目录呢
system("cd \\")也不行,为什么,如何调用system函数改变当前目录

[解决办法]
你编译好的程序,运行的时候,认为“当前目录”就是程序所在的目录。
用system(),即使执行了“cd”命令,执行完毕后,“当前目录”也没改变。

编写程序的时候只有“相对路径”和“绝对路径”的概念,别想着“改变路径”了

改变路径,是操作系统给非编程人员提供的便利
[解决办法]
_chdir, _wchdir
Change the current working directory.

int _chdir( const char *dirname );

int _wchdir( const wchar_t *dirname );

Routine Required Header Optional Headers Compatibility
_chdir <direct.h> <errno.h> Win 95, Win NT
_wchdir <direct.h> or <wchar.h> <errno.h> Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

Each of these functions returns a value of 0 if successful. A return value of 1 indicates that the specified path could not be found, in which case errno is set to ENOENT.

Parameter

dirname

Path of new working directory

Remarks

The _chdir function changes the current working directory to the directory specified by dirname. The dirname parameter must refer to an existing directory. This function can change the current working directory on any drive and if a new drive letter is specified in dirname, the default drive letter will be changed as well. For example, if A is the default drive letter and \BIN is the current working directory, the following call changes the current working directory for drive C and establishes C as the new default drive:

_chdir("c:\\temp");

When you use the optional backslash character (\) in paths, you must place two backslashes (\\) in a C string literal to represent a single backslash (\).

_wchdir is a wide-character version of _chdir; the dirname argument to _wchdir is a wide-character string. _wchdir and _chdir behave identically otherwise.

Generic-Text Routine Mapping:

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tchdir _chdir _chdir _wchdir


Example

/* CHGDIR.C: This program uses the _chdir function to verify
* that a given directory exists.
*/

#include <direct.h>
#include <stdio.h>
#include <stdlib.h>

void main( int argc, char *argv[] )
{
if( _chdir( argv[1] ) )
printf( "Unable to locate the directory: %s\n", argv[1] );
else
system( "dir *.wri");
}


Output

Volume in drive C is CDRIVE
Volume Serial Number is 0E17-1702

Directory of C:\write

04/21/95 01:06p 3,200 ERRATA.WRI
04/21/95 01:06p 2,816 README.WRI
2 File(s) 6,016 bytes
71,432,116 bytes free


Directory Control Routines

See Also _mkdir, _rmdir, system

[解决办法]
BOOL SetCurrentDirectory(
LPCTSTR lpPathName
);

C/C++ code
#include <windows.h> #include <stdio.h>#include <conio.h>#include <tchar.h>#define BUFSIZE MAX_PATH int _tmain(int argc, TCHAR **argv, TCHAR **envp) {    TCHAR Buffer[BUFSIZE];   DWORD dwRet;   if(argc != 2)   {      _tprintf(TEXT("Usage: Test <dir>\n"));      return 0;   }   dwRet = GetCurrentDirectory(BUFSIZE, Buffer);   if( dwRet == 0 )   {      _tprintf(TEXT("GetCurrentDirectory failed (%d)\n"), \          GetLastError());      return 0;   }   if(dwRet > BUFSIZE)   {      _tprintf(TEXT("GetCurrentDirectory failed (buffer too small; \         need %d chars)\n"), dwRet);      return 0;   }   if( !SetCurrentDirectory(argv[1]))   {      _tprintf(TEXT("SetCurrentDirectory failed (%d)\n"), \         GetLastError());      return 0;   }   _tprintf(TEXT("Set current directory to %s\n"), argv[1]);   if( !SetCurrentDirectory(Buffer) )   {      _tprintf(TEXT("SetCurrentDirectory failed (%d)\n"), \         GetLastError());      return 0;   }   _tprintf(TEXT("Restored previous directory (%s).\n"), Buffer);   return 1;     } 


[解决办法]
system() 创建子进程后将命令交于 shell 执行,那改变的只是作为子进程的 shell 的东西。

读书人网 >C语言

热点推荐