c++ - 如何以更有效的方式在 C++ 中创建文件夹?

标签 c++ directory

在我的 C++ 项目中,我提供了一个选项来备份为存储记录而创建的文件,它通过在该目录中创建一个文件夹来备份用户给定路径中的文件。其代码如下:

void backup()
{
 char a[40],c[40],b[40];
 product p1;
 clrscr();
 ifstream fp1("products.dat", ios::binary);
 ifstream fp2("purchase.dat",ios::binary);
 ifstream fp3("sales.dat",ios::binary);
 if(fp1==NULL || fp2==NULL || fp3==NULL)
 {
  cout<<"\n\tError-No or Incomplete Database...";
 }
 else
 {
  cout<<d_line;
  cout<<"\t\t\t       Database backup";
  cout<<"\n\t\t\t       -------- ------";
  cout<<line;
  cout<<"\n\tEnter the Directory in which you want to create backup:";
  gets(a);
  strcpy(b,a);
  strcpy(c,a);
  strcat(a,"/backup_b");
  strcat(b,"/backup_b");
  strcat(c,"/backup_b");
  mkdir(a);
  strcat(a,"/products.dat");
  strcat(b,"/purchase.dat");
  strcat(c,"/sales.dat");
  ofstream fp1_t(a, ios::binary | ios::trunc);
  ofstream fp2_t(b, ios::binary | ios::trunc);
  ofstream fp3_t(c, ios::binary | ios::trunc);

  if(fp1_t==NULL || fp2_t==NULL ||fp3_t==NULL)
  {
   cout<<"\n\n\tError During creating backup...\n";
  }
  else
  {
   while(!fp1.eof())
   {
    fp1.read((char *) &p1,sizeof(struct product));
    if(fp1.fail())
    {
     break;
    }
    else
    {
     fp1_t.write((char *) &p1,sizeof(struct product));
    }
   }
   while(!fp2.eof())
   {
    fp2.read((char *) &p1,sizeof(struct product));
    if(fp2.fail())
    {
     break;
    }
    else
    {
     fp2_t.write((char *) &p1,sizeof(struct product));
    }
   }
   while(!fp3.eof())
   {
    fp3.read((char *) &p1,sizeof(struct product));
    if(fp3.fail())
    {
     break;
    }
    else
    {
     fp3_t.write((char *) &p1,sizeof(struct product));
    }
   }
   fp1_t.close();
   fp2_t.close();
   fp3_t.close();
  }
  fp1.close();
  fp2.close();
  fp3.close();
  cout<<line;
  cout<<"\n\tBackup Created Successfully...";
 }
 cout<<line<<conti;
 getch();
}

我想知道,在 C++ 中创建文件夹是否有比我正在做的更有效的方法?

最佳答案

当然有更多独立于平台的方式来做到这一点,以及更多类型安全的方式。尝试 boost filesystem .

关于c++ - 如何以更有效的方式在 C++ 中创建文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11227814/

相关文章:

c++ - 哪种语言和库更适合高性能网络设备轮询服务器 (SNMP)?

c++ - 从 IDirect3DSurface9 获取像素?

c++ - C++ shared_ptr::operator* 危险吗?

c++ - 有没有办法配置MSVS静态代码分析的细节?

带有图像文件的 Java 项目

ruby - 遍历一个目录中的每个文件

c++ - 我不明白如何在 C++ 中创建和使用动态数组

view - 项目 View 中的 IntelliJ IDEA 文件夹而不是包

c++ - 如何使用 C++ 复制目录

c# - MemoryMappedFiles 找不到路径的一部分