0
我试图让给出的路径名对应的/dev
文件,有点像df
命令如何在UNIX中将路径映射到相应的/ dev文件 - C++?
我目前使用这个命令,但我宁可不使用system()
功能或特定系统的命令。
下面是当前的代码:
const string PathStr (Path); // Path is input value
// get the /dev/* files
string Cmd ("df 2> /dev/null | grep /dev | awk '{print $1, $6}'");
FILE * Pipe = popen (Cmd.c_str(), "r");
if (!Pipe)
{
errorLog("Cannot execute command : \"" + Cmd + '"');
return string();
}
char Buf [BufSz];
stringstream sstr;
while (!feof(Pipe))
{
if (fgets(Buf, BufSz, Pipe))
sstr << Buf;
}
pclose(Pipe);
// get the actual /dev file
pair<string, string> DiskPaths;
while (!sstr.eof())
{
string Dev, MountPoint;
sstr >> Dev >> MountPoint;
if (string::npos != PathStr.find(MountPoint) &&
MountPoint.size() > DiskPaths.second.size())
DiskPaths = make_pair(Dev, MountPoint);
}
对,'stat()'技巧似乎做的事情,但它的很多实现继续使用'df'。因此,'df'也读取'/ etc/mtab'文件,但我想它比我自己做的更可靠。 – perelo