liyanbo的个人博客分享 http://blog.sciencenet.cn/u/liyanbo

博文

C++文件夹存在,删除,判断存在

已有 4543 次阅读 2015-7-29 10:52 |个人分类:C++|系统分类:教学心得

1、新建文件夹

http://pubs.opengroup.org/onlinepubs/009695399/functions/mkdir.html

 

#include <sys/stat.h>
int mkdir(const char *path, mode_tmode);

下面的例子建立了一个名子为/home/cnd/mod1的目录,具有  read/write/search permissions for ownerand group, and with read/search permissions for others.

#include <sys/types.h>

#include <sys/stat.h>

int status;

...

status = mkdir("/home/cnd/mod1", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);


新建成功返回0,否则返回-1.


2、判读文件是否具有某种权限或文件夹是否存在

http://blog.csdn.net/roger_77/article/details/1538447/

利用 c 语言的库的办法:函数名: access
功  能: 确定文件的访问权限
用  法: int access(const char *filename, int amode);

mode Value            Checks File For
00                              Existence only
02                              Write permission
04                              Read permission
06                              Read and write permission


例子如下

#include  <io.h>

#include  <stdio.h>

#include  <stdlib.h>


void main( void )
{
 
/* Check for existence */
 
if( (access( "ACCESS.C", 0 )) != -1 )
  {
     printf(
"File ACCESS.C exists " );
     
/* Check for write permission */
     
if( (access( "ACCESS.C", 2 )) != -1 )
        printf(
"File ACCESS.C has write permission " );
  }

}

返回0,存在或是具有某权限;否则返回-1.


3、删除文件夹或文件


利用命令行给出删除指令:

例子1:

#include <stdlib.h>

int main()
{
   system("del *.txt"); //可以增加,如system("del *.txt *.tmp");
   return 0;
}
 
例子2:
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
   const char* pathToDelete = arg[1];

   char cmd[128];
   sprintf(cmd, "rm -r %s", pathToDelete);
   system(cmd);
   return 0;
}





https://blog.sciencenet.cn/blog-1515646-909072.html

上一篇:师姐代码心得-1解析命令行
下一篇:leetcode-Single Number1,2,3
收藏 IP: 159.226.43.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-7-17 23:17

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部