标签:
./a.out .
表示循环播放当前目录下的音乐
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<dirent.h>
#include<string.h>
int main(int argc,char *argv[])
{
DIR *dir;
struct dirent *ent;
char str[32][128];
char str1[128];
char str2[128];
memset(str,0,sizeof(str));
dir=opendir(argv[1]);
if(NULL==dir)
{
perror("opendir");
return;
}
int k=0;
while(ent=readdir(dir))
{
if(ent->d_type==4)
{
continue;
}
strcpy(str1,ent->d_name);
strcpy(str2,str1+strlen(str1)-4);
if(strcmp(str2,".mp3")==0)
{
sprintf(str[k],"%s/%s",argv[1],ent->d_name);
printf("%s\n",str[k]);
k++;
}
}
closedir(dir);
int len=k;
k=0;
while(1)
{
pid_t pid;
pid=fork();
if(pid==-1)
{
perror("fork");
exit(-1);
}
if(pid==0)
{
execl("/usr/bin/madplay","madplay",str[k],NULL);
}
waitpid(pid,NULL,0);
k++;
if(k==len)
{
k=0;
}
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/liudehao/p/5754390.html