关闭

C/C++文件夹 的操作

标签: 字体dos
503人阅读 评论(0) 收藏 举报
分类:

通过system();函数调用DOS命令来创建文件夹和删除文件、文件夹。创建文件可以用其文件操作实现。 

头文件在stdlib.h或者process.h

system("md c:\\mydir");//创建一个文件夹 
system("rd c:\\mydir");//删除一个文件夹 
system("del c:\\test\\myfile.dat");//删除一个文件。


标准C++本身不能创建文件夹,但不同编译器本身对这个功能都做了扩展,VC使用 _mkdir("mydir")函数来创建

(#include <direct.h>),TC使用mkdir("mydir")来创建(#include <dir.h>)。 
如果只是创建文件夹,还可以利用system("md mydir")函数来解决;

摘自MSDN:

Creates a new directory.

int _mkdir( const char *dirname );

int _wmkdir(const wchar_t *dirname );

Parameters
dirname
Path for a new directory

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.

For more information about these and other return codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

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.


Example
// crt_makedir.c

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

int 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" );
}

Sample Output
Directory '\testtmp' was successfully created
 Volume in drive C has no label.
 Volume Serial Number is E078-087A

 Directory of C:\testtmp

02/12/2002  09:56a      <DIR>          .
02/12/2002  09:56a      <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  15,498,690,560 bytes free
Directory '\testtmp' was successfully removed

对文件的删除:C++ 也有 

#include <stdio.h> 
void main( void ) 

{ 

if( remove( "remove.obj" ) == -1 ) 

perror( "Could not delete 'REMOVE.OBJ'" ); 

else 

printf( "Deleted 'REMOVE.OBJ'\n" ); 

}

添加内容:

注意问题:再写目录的时候要注意E:\\123.txt(两条反斜杠),再者我是直接在e盘文件建立一个命名为123.txt的文件,这里有个问题。在我的电脑里面,若代码为system("del E:\\123.txt");这是无法删除成功的。

原因在于:对于这个文件系统是隐藏其后缀文件格式的,它的全名应为123.txt.txt.若改为这样即可成功


    system("cd.>E:\\123.txt");//建立一个空文件
	system("del E:\\123.txt");//删除文件





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:70059次
    • 积分:1474
    • 等级:
    • 排名:千里之外
    • 原创:78篇
    • 转载:16篇
    • 译文:0篇
    • 评论:8条
    最新评论