user-0 提问: C语言更改当前目录问题

回复:
user: user-1
content: _getcwd, _wgetcwd Get the current working directory.  char *_getcwd( char *buffer, int maxlen );  wchar_t *_wgetcwd( wchar_t *buffer, int maxlen );  Routine Required Header Compatibility  _getcwd <direct.h> Win 95, Win NT  _wgetcwd <direct.h> or <wchar.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 pointer to buffer. A NULL return value indicates an error, and errno is set either to ENOMEM, indicating that there is insufficient memory to allocate maxlen bytes (when a NULL argument is given as buffer), or to ERANGE, indicating that the path is longer than maxlen characters.  Parameters  buffer  Storage location for path  maxlen  Maximum length of path in characters: char for _getcwd and wchar_t for _wgetcwd  Remarks  The _getcwd function gets the full path of the current working directory for the default drive and stores it at buffer. The integer argument maxlen specifies the maximum length for the path. An error occurs if the length of the path (including the terminating null character) exceeds maxlen. The buffer argument can be NULL; a buffer of at least size maxlen (more only if necessary) will automatically be allocated, using malloc, to store the path. This buffer can later be freed by calling free and passing it the _getcwd return value (a pointer to the allocated buffer).  _getcwd returns a string that represents the path of the current working directory. If the current working directory is the root, the string ends with a backslash ( \ ). If the current working directory is a directory other than the root, the string ends with the directory name and not with a backslash.  _wgetcwd is a wide-character version of _getcwd; the buffer argument and return value of _wgetcwd are wide-character strings. _wgetcwd and _getcwd behave identically otherwise.  Generic-Text Routine Mappings  TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined  _tgetcwd _getcwd _getcwd _wgetcwd    Example  // GETCWD.C /* This program places the name of the current directory in the   * buffer array, then displays the name of the current directory   * on the screen. Specifying a length of _MAX_PATH leaves room   * for the longest legal path name.  */  #include <direct.h> #include <stdlib.h> #include <stdio.h>  void main( void ) {    char buffer[_MAX_PATH];     /* Get the current working directory: */    if( _getcwd( buffer, _MAX_PATH ) == NULL )       perror( "_getcwd error" );    else       printf( "%s\n", buffer ); }   Output  C:\code   Directory Control Routines  See Also   _chdir, _mkdir, _rmdir

user: user-2
content: _chdir, _wchdir Change the current working directory.  int _chdir( const char *dirname );  int _wchdir( const wchar_t *dirname );   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

user: user-2
content: chdir() Change the current working directory  Synopsis: #include <unistd.h>  int chdir( const char* path );Arguments: path  The new current working directory.  Library: libc  Use the -l c option to qcc to link against this library. This library is usually included automatically.   Description: The chdir() function changes the current working directory to path, which can be relative to the current working directory or an absolute path name.  Returns: 0  Success.  -1  An error occurred (errno is set).  Errors: EACCES  Search permission is denied for a component of path.  ELOOP  Too many levels of symbolic links or prefixes.  ENAMETOOLONG  The path argument is longer than PATH_MAX, or a pathname component is longer than NAME_MAX.  ENOENT  The specified path doesn't exist, or path is an empty string.  ENOMEM  There wasn't enough memory to allocate a control structure.  ENOSYS  The chdir() function isn't implemented for the filesystem specified in path.  ENOTDIR  A component of path is not a directory.  Examples: #include <stdio.h> #include <stdlib.h> #include <unistd.h>  int main( int argc, char* argv[] ) {     if( argc != 2 ) {         fprintf( stderr, "Use: cd <directory>\n" );         return EXIT_FAILURE;     }      if( chdir( argv[1] ) == 0 ) {         printf( "Directory changed to %s\n", argv[1] );         return EXIT_SUCCESS;     } else {         perror( argv[1] );         return EXIT_FAILURE;     } }

user: user-3
content: 不懂windows编程~