肥宅自学平台_人人成为技术开发者

肥宅人只等待您,联系我们吧,曾经的巅峰是否能回来?我们正在用心服务!

C++ File文件处理 删除文件和文件夹目录

 更新时间:2023-09-12 08:47:18   作者:肥宅-季波   我要评论(0)  

         在C++程序开发中,也会遇到很多文件上传,文件写入等对于文件的操作业务需要开发,文件处理也是任何应用程序的重要组成部分。C++有几种创建,读取,更新和删除文件的方法。本文主要介绍C++ File文件操作删除文件和目录。

1、删除文件
要使用C++ 删除文件,需要使用int remove(const char * filename);方法,filename为要删除的文件名,可以为一目录。如果参数filename 为一文件,则调用unlink()处理;若参数filename 为一目录,则调用rmdir()来处理。删除成功则返回0,失败则返回-1,错误原因存于errno。C++中头文件是#include <cstdio>。

例如,
  1. #include<iostream>
  2. #include<cstdio>
  3. #include <string.h>
  4. using namespace std;
  5. int main()
  6. {
  7.     char *savePath = "/home/cjavapy/hello.txt";
  8.     if(remove(savePath)==0)
  9.     {
  10.         cout<<"删除成功"<<endl;
  11.     }
  12.     else
  13.     {
  14.         cout<<"删除失败"<<endl;
  15.     }
  16.     //输出错误信息
  17.     cout << strerror(errno);
  18.     return 0;
  19. }
错误代码:

1)EROFS  欲写入的文件为只读文件。

2)EFAULT  参数filename 指针超出可存取内存空间。

3)ENAMETOOLONG  参数filename 太长。

4)ENOMEM  核心内存不足。

5)ELOOP  参数filename 有过多符号连接问题。

6)EIO I/O存取错误。

2、删除文件夹
除了能删除文件,也可以使用int rmdir( const char *dirname );删除文件夹。删除成功则返回0,失败则返回-1,错误原因存于errno。但是,删除的文件夹必须为空:

例如,
  1. #include <unistd.h>
  2. #include <stdlib.h> /* for system()函数 */
  3. #include<iostream>
  4. #include<cstdio>
  5. #include <string.h>
  6. using namespace std;
  7. int main( void )
  8. {
  9.   system("mkdir mydir");
  10.   system("ls -l mydir");
  11.   //getchar();
  12.   printf("%s","删除文件夹\n");
  13.   cout << "返回值:" << rmdir("mydir") <<endl;
  14.   //输出错误信息
  15.   cout << strerror(errno);
  16.   return(0);
  17. }
  1. #include <stdio.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <dirent.h>
  7. #include <string.h>
  8. using namespace std;
  9. void error_quit( const char *msg )
  10. {
  11.   perror( msg );
  12.   exit( -1 );
  13. }
  14. void change_path( const char *path )
  15. {
  16.   printf( "Leave %s Successed . . .\n", getcwd( NULL, 0 ) );
  17.   if ( chdir( path ) == -1 )
  18.     error_quit( "chdir" );
  19.   printf( "Entry %s Successed . . .\n", getcwd( NULL, 0 ) );
  20. }
  21. void rm_dir( const char *path )
  22. {
  23.   DIR    *dir;
  24.   struct dirent  *dirp;
  25.   struct stat  buf;
  26.   char    *p = getcwd( NULL, 0 );
  27.   if ( (dir = opendir( path ) ) == NULL )
  28.     error_quit( "OpenDir" );
  29.   change_path( path );
  30.   while ( dirp = readdir( dir ) )
  31.   {
  32.     if ( (strcmp( dirp->d_name, "." ) == 0) || (strcmp( dirp->d_name, ".." ) == 0) )
  33.       continue;
  34.     if ( stat( dirp->d_name, &buf ) == -1 )
  35.       error_quit( "stat" );
  36.     if ( S_ISDIR( buf.st_mode ) )
  37.     {
  38.       rm_dir( dirp->d_name );
  39.       /*if(rmdir(dirp->d_name)==-1)
  40.        *  error_quit("rmdir");
  41.        * printf("rm %s Successed . . .\n",dirp->d_name);*/
  42.       continue;
  43.     }
  44.     if ( remove( dirp->d_name ) == -1 )
  45.       error_quit( "remove" );
  46.     printf( "rm %s Successed . . .\n", dirp->d_name );
  47.   }
  48.   closedir( dir );
  49.   change_path( p );
  50.   if ( rmdir( path ) == -1 )
  51.     error_quit( "rmdir" );
  52.   printf( "rm %s Successed . . .\n", path );
  53. }
  54. int main( int argc, char **argv )
  55. {
  56. // if ( argc < 2 )
  57.   //{
  58.     //fprintf( stderr, "<用法>: %s + pathname", argv[0] );
  59.     //return(-1);
  60.   //}
  61.   rm_dir("/tmp/");
  62.   return(0);
  63. }
"小礼物走一走,来肥宅自学平台支持我"
评论区

评论

共条评论
  • 这篇文章还没有收到评论,赶紧来抢沙发吧~
客服中心在线客服
全心全意为平台用户服务
Copyright © 2017-2023 自学平台网站地图:去查看>
  • 肥宅人自己的自学平台
  • 人人都能成为平台讲师
  • 贡献你的力量壮大肥宅自学平台
  • 将自己的能力变现
  • 实现人生的第二职业
网址收藏平台安卓APP
微信公众号微信公众号

本站部分图片或者资源来自程序自动采集或卖家(商家)发布,如果侵犯了您的权益请与我们联系,我们将在24小时内删除!谢谢!

肥宅自学教程网是一个主打IT视频教程、自媒体运行、摄影剪辑等内容的资源学习交流平台。