C/C++文件夹 的操作

通过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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值