我正在编写一个可以在 PC 上创建/删除/重命名/搜索文件和目录的小类。
我成功编写了课程并在 Linux 上运行。
当我试图在 MinGW 中运行相同的类代码时,它给出了一个错误。我可以缩小到:Linux 中的 mkdir 函数,Cygwin 有 2 个参数(目录名、模式权限),但在 MinGW 中只有一个参数(目录名)。
我的查询是:a)使代码在两个操作系统上工作的最佳方法是什么。b) 虽然我从未使用过,但我听说预处理器指令可以像 #ifdefined .....#endif .. 或类似的东西 c) 使用预处理器指令是一种很好的编程习惯。据我所知,应尽量少使用预处理器指令。
有人可以帮助我吗:
这是我在 Linux 和 Cygwin 上运行的代码:
#include "BioDatabase.h"
#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
BioDatabase::BioDatabase() {
string s = getcwd(NULL,0);
changeDirectory(s,"*");
}
BioDatabase::BioDatabase(string directoryName, string extension)
{
changeDirectory(directoryName, extension);
}
bool BioDatabase::createDirectory(string st)
{
if( mkdir(st.c_str(),0755) == -1)
{
cerr <<endl<<"BOSERR-BioDatabase, createDirectory: Path or file function not found or Permission denied\n\n";
return false;
}
flag =1;
return true;
}