做了一个原型你,其不安全的方式,没有错误检查,但它的工作原理。
代码:
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
int scan(std::string dir, std::vector<std::string> &files)
{
DIR* dr = opendir(dir.c_str());
struct dirent *drp;
while ((drp = readdir(dr)) != NULL)
{
struct stat s;
stat((dir + "/" + std::string(drp->d_name)).c_str(), &s);
if (s.st_mode & S_IFREG)
{
files.push_back(std::string(drp->d_name));
}
}
closedir(dr);
return 0;
}
int main()
{
std::string dir = ".", cmd = "vlc";
std::vector<std::string> files, vfiles;
scan(dir, files);
for (unsigned int i = 0; i < files.size(); i++)
{
if (files[i].substr(files[i].find(".")) == ".mp3")
{
vfiles.push_back(std::string(files[i]));
}
}
for (unsigned int i = 0; i < vfiles.size(); i++)
{
cmd += " " + dir + "/" + vfiles[i];
}
printf("%s\n", cmd.c_str());
system(cmd.c_str());
return 0;
}
输出:
vlc ./test.mp3 ./test2.mp3
它所做的是:它会列出指定文件夹中的所有文件"."
默认情况下,它会检查该文件实际上是一个是文件不是文件夹,它会列出所有以".mp3"
结尾的文件,然后运行vlc file1.mp3 file2.mp3 file4.mp3
。 VLC将按顺序播放所有列出的文件。
在将Windows 8添加到Path
后使用(VLC media player 2.0.6 Twoflower
)。
[可能的重复](http://stackoverflow.com/questions/6143100/how-do-i-open-a-file-in-its-default-program-linux)。那么,不完全相同,但可能是你的意思(如果他们不使用vlc呢?) – BoBTFish
@BoBTFish:Vlc在这种情况下是特定的 – Rishabh
[''g_app_info_launch'](https://developer.gnome.org/ GIO/2.32/GAppInfo.html#G-APP-信息推出)? – BoBTFish