【问题标题】:Reading ID3V2 TAG in c languagec语言读取ID3V2 TAG
【发布时间】:2013-02-06 02:22:17
【问题描述】:

我是一名编程初学者,我的大学有一项任务是编写 C 代码,用于读取 ID3V2 格式的 mp3 文件。我已经在网上搜索并尝试将一些部分放在一起,然后我想出了这段代码。尽管我从上到下放置了许多错误警告,但我仍然无法理解错误在哪里。

我的第一个问题是它编译的代码只有一个警告: 警告:格式“%s”需要匹配的“char *”参数 [-Wformat]

不幸的是,我不知道我的错误是什么。我正在尝试打印用户的输入以防出现错误,我认为这样做会收到此警告。

我的第二个问题是当我执行作为输出得到的代码时: 程序获取标签失败

任何帮助将不胜感激。

提前感谢您抽出宝贵时间阅读我的问题。

最好的问候, 灭霸

这是我目前的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    typedef struct{
    char tag[3];
    char title[30];
    char artist[30];
    char album[30];
    char year[4];
    char comment[30];
    unsigned char genre;
    }mp3Info;

    int main (int argc, char *argv[])
    {
    if ( argc != 1 ) /* Input from user argc should be 1 for correct
    *execution. The argument count variable
    * stores the number of arguments plus one. */ 
    {
    /*We print argv[0], if the program that user has chosen for
     * execution it is not correct as input if has more inputs*/ 
    printf( "Please choose one file: %s <silence.mp3> \n", argv[0] );
    }
    else 
    {
    FILE *file; /* A file pointer is a variable of type FILE, 
    * we declare a file pointer.*/

    file = fopen("silence.mp3", "rb"); /* Open the file
    * silence.mp3 in
    * "reading" mode "r". */

    if (file == NULL) {
    /* If the file could not open for a variety of reasons
     * the program should inform us by print the document
     * and exit.*/ 
    printf("I couldn't open: %s for reading.\n");
    exit(0);
    }

    else 
    {
    mp3Info tag_in;

    /* Set the fseek to the beggining of the document with
     * the help of the command SEEK_SET, condition if fail*/

    if (fseek(file, 0 *sizeof(mp3Info), SEEK_SET) == -1)
    {
    fprintf(stderr, "Not able to fseek");
    return EXIT_FAILURE;
    }

    /* Read the first name "tag" */
    if (fread(&tag_in, sizeof(mp3Info), 1, file) != 1)
    {
    printf("Could not read the tag\n");
    exit (0);
    }

    /* Compare memory areas for verification */

    if (memcmp(tag_in.tag, "TAG", 3) == 0)
    {
    /* Verify that names are where as we expect */
    printf("Title: %.30s\n", tag_in.title);
    printf("Artist: %.30s\n", tag_in.artist);
    printf("Album: %.30s\n", tag_in.album);
    printf("Year: %.4s\n", tag_in.year);

    if (tag_in.comment[28] == '\0')

    {
    printf("Comment: %.28s\n", tag_in.comment);
    printf("Track: %d\n", tag_in.comment[29]);
    }

    else
    {
    printf("Comment: %.30s\n", tag_in.comment);
    }
    printf("Genre: %d\n", tag_in.genre);
    }

    else
    {
    fprintf(stderr, "The program has failed to get the Tags\n");
    return EXIT_FAILURE;
    }

    fclose(file);
    return 0;

    }
    }
    }

【问题讨论】:

  • 您将 ID3v1 与 ID3v2 格式混淆了。

标签: c id3v2


【解决方案1】:

您收到的警告与此行有关(您可以从错误消息中遗漏的行号确定):

printf("I couldn't open: %s for reading.\n");

您使用 %s 指定将作为参数传递的字符串,但您没有传递您承诺的字符串。

您的程序失败表明您提供给它的文件在您期望的位置没有 ID3 标记。这是因为您尝试读取的 ID3v1 标记(不是 ID3v2;这是一种完全不同的格式)通常位于文件的 end 处,而不是开头。要阅读它,您需要寻找到文件末尾附近,而不是开头:

fseek(file, -sizeof(mp3Info), SEEK_END);

【讨论】:

  • 亲爱的duskwuff,感谢您立即回复我的问题。我正在尝试编写代码来读取 ID3V2 mp3 文件。如果我没记错的话,标签的信息在文件的开头。这就是我使用'(fseek(file, 0 *sizeof(mp3Info), SEEK_SET) == -1)'的原因。如果我确实解释正确我正在尝试创建 ID3v2 代码,我很抱歉。
  • 您在mp3Info 中指定的结构是一个ID3v1 标记。 ID3v2 标记要复杂得多,并且没有固定的大小或顺序。
  • 亲爱的duskwuff,再次感谢您抽出宝贵时间阅读并回复我的请求。我已经向一位朋友寻求一些指导,他建议我最好的来源是先阅读以制作它,然后逐步尝试实施它。 link。再次感谢您的时间和精力。
猜你喜欢
  • 2013-04-30
  • 1970-01-01
  • 2020-11-08
  • 2014-12-13
  • 2011-03-01
  • 2011-12-08
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
相关资源
最近更新 更多