作者:Jessica | 来源:互联网 | 2023-05-17 09:10
如何编程实现 读取 dir /s 产生的文件 还原目录结构
就是有那个文件 可以把目录结构还原出来
8 个解决方案
在本ID眼中,调用shell命令实现和编程实现没有区别。
_splitpath, _wsplitpath
Break a path name into components.
void _splitpath( const char *path, char *drive, char *dir, char *fname, char *ext );
void _wsplitpath( const wchar_t *path, wchar_t *drive, wchar_t *dir, wchar_t *fname, wchar_t *ext );
Routine Required Header Compatibility
_splitpath Win 95, Win NT
_wsplitpath or Win 95, 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
None
Parameters
path
Full path
drive
Optional drive letter, followed by a colon (:)
dir
Optional directory path, including trailing slash. Forward slashes ( / ), backslashes ( \ ), or both may be used.
fname
Base filename (no extension)
ext
Optional filename extension, including leading period (.)
Remarks
The _splitpath function breaks a path into its four components. _splitpath automatically handles multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the multibyte code page currently in use. _wsplitpath is a wide-character version of _splitpath; the arguments to _wsplitpath are wide-character strings. These functions behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tsplitpath _splitpath _splitpath _wsplitpath
Each argument is stored in a buffer; the manifest constants _MAX_DRIVE, _MAX_DIR, _MAX_FNAME, and _MAX_EXT (defined in STDLIB.H) specify the maximum size necessary for each buffer. The other arguments point to buffers used to store the path elements. After a call to _splitpath is executed, these arguments contain empty strings for components not found in path. You can pass a NULL pointer to _splitpath for any component you don’t need.
Example
/* MAKEPATH.C */
#include
#include
void main( void )
{
char path_buffer[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
_makepath( path_buffer, "c", "\\sample\\crt\\", "makepath", "c" );
printf( "Path created with _makepath: %s\n\n", path_buffer );
_splitpath( path_buffer, drive, dir, fname, ext );
printf( "Path extracted with _splitpath:\n" );
printf( " Drive: %s\n", drive );
printf( " Dir: %s\n", dir );
printf( " Filename: %s\n", fname );
printf( " Ext: %s\n", ext );
}
Output
Path created with _makepath: c:\sample\crt\makepath.c
Path extracted with _splitpath:
Drive: c:
Dir: \sample\crt\
Filename: makepath
Ext: .c
File Handling Routines
See Also _fullpath, _getmbcp, _makepath, _setmbcp
_mkdir, _wmkdir
Create a new directory.
int _mkdir( const char *dirname );
int _wmkdir( const wchar_t *dirname );
Routine Required Header Compatibility
_mkdir Win 95, Win NT
_wmkdir or 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 the value 0 if the new directory was created. On an error the function returns –1 and sets errno as follows:
EEXIST
Directory was not created because dirname is the name of an existing file, directory, or device
ENOENT
Path was not found
Parameter
dirname
Path for new directory
Remarks
The _mkdir function creates a new directory with the specified dirname. _mkdir can create only one new directory per call, so only the last component of dirname can name a new directory. _mkdir does not translate path delimiters. In Windows NT, both the backslash ( \) and the forward slash (/ ) are valid path delimiters in character strings in run-time routines.
_wmkdir is a wide-character version of _mkdir; the dirname argument to _wmkdir is a wide-character string. _wmkdir and _mkdir behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tmkdir _mkdir _mkdir _wmkdir
Example
/* MAKEDIR.C */
#include
#include
#include
void main( void )
{
if( _mkdir( "\\testtmp" ) == 0 )
{
printf( "Directory '\\testtmp' was successfully created\n" );
system( "dir \\testtmp" );
if( _rmdir( "\\testtmp" ) == 0 )
printf( "Directory '\\testtmp' was successfully removed\n" );
else
printf( "Problem removing directory '\\testtmp'\n" );
}
else
printf( "Problem creating directory '\\testtmp'\n" );
}
Output
Directory '\testtmp' was successfully created
Volume in drive C is CDRIVE
Volume Serial Number is 0E17-1702
Directory of C:\testtmp
05/03/94 12:30p .
05/03/94 12:30p ..
2 File(s) 0 bytes
17,358,848 bytes free
Directory '\testtmp' was successfully removed
Directory Control Routines
See Also _chdir, _rmdir
strtok, wcstok, _mbstok
Find the next token in a string.
char *strtok( char *strToken, const char *strDelimit );
wchar_t *wcstok( wchar_t *strToken, const wchar_t *strDelimit );
unsigned char *_mbstok( unsigned char*strToken, const unsigned char *strDelimit );
Routine Required Header Compatibility
strtok ANSI, Win 95, Win NT
wcstok or ANSI, Win 95, Win NT
_mbstok Win 95, 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
All of these functions return a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.
Parameters
strToken
String containing token(s)
strDelimit
Set of delimiter characters
Remarks
The strtok function finds the next token in strToken. The set of characters in strDelimit specifies possible delimiters of the token to be found in strToken on the current call. wcstok and _mbstok are wide-character and multibyte-character versions of strtok. The arguments and return value of wcstok are wide-character strings; those of _mbstok are multibyte-character strings. These three functions behave identically otherwise.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tcstok strtok _mbstok wcstok
On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.
Warning Each of these functions uses a static variable for parsing the string into tokens. If multiple or simultaneous calls are made to the same function, a high potential for data corruption and inaccurate results exists. Therefore, do not attempt to call the same function simultaneously for different strings and be aware of calling one of these function from within a loop where another routine may be called that uses the same function. However, calling this function simultaneously from multiple threads does not have undesirable effects.
Example
/* STRTOK.C: In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/
#include
#include
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;
void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}
Output
A string of ,,tokens
and some more tokens
Tokens:
A
string
of
tokens
and
some
more
tokens
String Manipulation Routines
See Also strcspn, strspn, setlocale