2013-01-23 49 views
0

代碼:afplay錯誤:AudioFileOpen失敗(-43)

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

int main (int argc, char *argv[]) { 
    char folderPath[1024]; 
    int i = 0; 
    for (i; i < (strlen(argv[0]) - 7); i++) { 
     folderPath[i] = argv[0][i]; 
    } 
    printf("Command: afplay %ssong.mp3\n", folderPath); 
    system("afplay %ssong.mp3", folderPath); 
    return 0; 
} 

所有輸出:

Command: afplay /Users/carloabelli/Desktop/FUNNY/song.mp3 
Error: AudioFileOpen failed (-43) 

當我從終端運行命令它完美。我想知道發生了什麼問題。

回答

3

system()不使用格式字符串。它將整個命令作爲文字字符串。使用sprintf()將命令格式化爲緩衝區,然後將該緩衝區發送給系統。

char buf[1024]; 
snprintf(buf, 1024, "afplay %ssong.mp3", folderPath); 
system(buf); 

或沿着這些線。