2013-02-06 182 views
-1

我是一名編程初學者,我從我的大學獲得了一份授權,用於讀取ID3V2格式的MP3文件。我搜索了網頁並試圖拼湊一些作品,然後我想出了這個代碼。雖然我已經從上到下提出了很多錯誤警告,但我仍然無法理解錯誤在哪裏。用c語言閱讀ID3V2標籤

我的第一個問題是,它只有一個警告編譯代碼: 警告:格式「%s」的期望匹配的「字符*」的說法[-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; 

    } 
    } 
    } 
+0

你把ID3v1與ID3v2格式混淆了。 –

回答

1

你得到的警告是在參照本線(你可以從線路數量決定你留出了錯誤消息):

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

你跟%s指定一個字符串作爲參數,BU傳遞你沒有通過你承諾的那個字符串。

程序的失敗表明您提供的文件沒有您期望的ID3標籤。這是因爲您試圖讀取的ID3v1標籤(不是ID3v2;這是完全不同的格式)通常位於文件的,而不是開頭。要閱讀它,你需要尋求接近文件的結尾,而不是開頭:

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

親愛的黃昏,謝謝你立即回覆我的問題。我正在嘗試編寫代碼來讀取ID3V2 mp3文件。如果我沒有弄錯,標籤的信息在文件的開頭。這就是爲什麼我用'(fseek(文件,0 * sizeof(mp3Info),SEEK_SET)== -1)'。如果我確實解釋了我正嘗試創建ID3v2代碼,我很抱歉Confusions。 – Thanos

+0

您在'mp3Info'中指定的結構是ID3v1標籤。 ID3v2標籤相當複雜,並且沒有固定的大小或順序。 – duskwuff

+0

親愛的黃昏,再次感謝您的閱讀和回覆我的請求。我已經要求朋友提供一些指導,他建議我最好的來源是首先閱讀,然後一步一步地嘗試實施它。 [鏈接](http://id3.org/id3v2.4.0-structure)。再次感謝您的時間和精力。 – Thanos