如何编写 C 程序在 Linux 上创建音乐播放列表

2024年 7月 16日 26.0k 0

如何编写 C 程序在 Linux 上创建音乐播放列表-1

使用我在 Linux 上制作的这个 C 程序在旅途中聆听你喜爱的歌曲。

我最近在 Linux 中编写了一个 C 程序,从我广泛的 MP3 库中创建一个较小的随机 MP3 文件选集。该程序会遍历一个包含我的 MP3 库的目录,然后创建一个包含随机的、较小的歌曲选集的目录。然后我将这些 MP3 文件复制到我的智能手机上,以便随时随地收听。

瑞典是一个人口稀少的国家,有许多农村地区没有完整的手机覆盖。这就是在智能手机上拥有 MP3 文件的原因之一。另一个原因是我并不总是有钱购买流媒体服务,所以我喜欢拥有自己喜欢的歌曲的副本。

你可以从它的 Git 仓库 下载我的应用。我专门为 Linux 编写了它,部分原因是在 Linux 上很容易找到经过良好测试的文件 I/O 例程。多年前,我尝试使用专有的 C 库在 Windows 上编写相同的程序,但在尝试文件复制时遇到了困难。Linux 使用户可以轻松直接地访问文件系统。

本着开源的精神,我没费多少力气就找到了 Linux 的文件 I/O 代码来激发我的灵感。我还发现了一些启发了我的分配内存的代码。我编写了随机数生成的代码。

该程序的工作方式如下所述:

  • 询问源目录和目标目录。
  • 询问存放 MP3 文件的目录下的文件个数。
  • 搜索你希望复制的收藏的百分比(从 1.0% 到 88.0%)。如果你有 1000 个文件的集合,并希望从你的集合中复制 125 个文件而不是 120 个文件,你也可以输入 12.5% 之类的数字。我将上限设置为 88%,因为复制超过 88% 的库将基本生成与你的基础库相似的库。当然,代码是开源的,因此你可以根据自己的喜好自由修改。
  • 使用指针和 malloc 分配内存。一些操作需要内存,包括代表音乐收藏中文件的字符串列表。还有一个列表来保存随机生成的数字。
  • 生成所有文件范围内的随机数列表(例如,如果集合有 1000 个文件,则为 1 到 1000)。
  • 复制文件。
  • 其中一些部分比其他部分更简单,但代码只有大约 100 行:

    #include
    #include
    #include
    #include /* include necessary header files */
    #include
    #include
    #include
    #include

    #define BUF_SIZE 4096 /* use buffer of 4096 bytes */
    #define OUTPUT_MODE 0700 /*protect output file */
    #define MAX_STR_LEN 256

    int main(void) {
    DIR *d;
    struct dirent *dir;
    char strTemp[256], srcFile[256],
    dstFile[256], srcDir[256], dstDir[256];
    char **ptrFileLst;

    char buffer[BUF_SIZE];
    int nrOfStrs=-1, srcFileDesc,
    dstFileDesc, readByteCount,
    writeByteCount, numFiles;
    int indPtrFileAcc, q;

    float nrFilesCopy;
    // vars for generatingRandNumList
    int i, k, curRanNum, curLstInd,
    numFound, numsToGen, largNumRange;
    int *numLst;

    float procFilesCopy;
    printf("Enter name of source Directory\n");
    scanf("%s", srcDir);
    printf("Enter name of destionation Directory\n");
    scanf("%s", dstDir);
    printf("How many files does the directory with mp3 files contain?\n");
    scanf("%d", &numFiles);
    printf("What percent of the files do you wish to make a random selection of\n");
    printf("enter a number between 1 and 88\n");
    scanf("%f", &procFilesCopy);

    // allocate memory for filesList, list of random numbers
    ptrFileLst= (char**) malloc(numFiles * sizeof(char*));

    for (i = 0; i < numFiles; i++) {
    ptrFileLst[i] = (char*)malloc(MAX_STR_LEN * sizeof(char));
    }

    largNumRange = numFiles;
    nrFilesCopy = (procFilesCopy / 100) * numFiles;

    numsToGen = (int)((procFilesCopy / 100) * numFiles);
    printf("nrFilesCopy=%f", nrFilesCopy);
    printf("NumsToGen=%d", numsToGen);
    numLst = malloc(numsToGen * sizeof(int));
    srand(time(0));

    numLst[0] = rand() % largNumRange + 1;
    numFound=0;
    do {
    curRanNum = (int)rand() % largNumRange + 1;
    if (numLst[0] == curRanNum) {
    numFound=1;
    }
    } while(numFound == 1);

    numLst[1] = curRanNum;
    getchar();
    curLstInd = 1;
    i = 0;
    while(1) {
    do {
    numFound = 0;
    curRanNum = (int)rand() % largNumRange + 1;
    for (int k = 0; k d_name);

    if (strTemp[0] != '.') {
    nrOfStrs++;
    strcpy(ptrFileLst[nrOfStrs], strTemp);
    }
    }
    closedir(d);
    }

    for (q = 0; q

    相关文章

    Linux 命令行的聊天工具 CenterIM
    Linux 桌面年仍未到来 但 Linux 移动之年已到来
    12 个在线学习 Linux 技能网站
    Linux Mint : 会是另一个新的 Ubuntu 吗?
    W3Conf 开发者大会将于下周召开
    Ubuntu 10.04 ARM 处理器上网本版本结束服务期

    发布评论