0

コード:

#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)

ターミナルからコマンドを実行すると、完全に機能します。私は何がうまくいかないのだろうと思っていました。

4

1 に答える 1

3

system()フォーマット文字列を使用しません。コマンド全体をリテラル文字列として受け取ります。sprintf()コマンドをバッファにフォーマットし、そのバッファをシステムに送信するために使用します。

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

またはこれらの線に沿った何か。

于 2013-01-23T23:29:36.847 に答える