2

私は完全に困惑しています。Linux でのMTPユニットのマウントを容易にするためのこの小さな機能がありますが、何らかの理由で、変数を使用しているときにlibnotifyにアイコンを表示させることができません。完全なパスをハードコーディングすると問題なく動作しますが、変数をgetcwdandとして使用すると表示されgetenvません。

コードの一部を次に示します。

char cwd[1024];
char *slash = "/";  
{
NotifyNotification *mount;
notify_init ("Galaxy Nexus mounter");
    if (getcwd(cwd, sizeof(cwd)) != NULL)
    {
        mount = notify_notification_new ("Samsung Galaxy Nexus", "Mounted at ~/Nexus", ("%s%sandroid_on.png", cwd, slash));
        fprintf(stdout, "Icon used %s%sandroid_on.png\n", cwd, slash);
        system("jmtpfs ~/Nexus");
        notify_notification_set_timeout (mount, 2000);
        notify_notification_show (mount, NULL);
    }
    }

私は何を間違っていますか?

4

2 に答える 2

1

これは正しくありません:

mount = notify_notification_new ("Samsung Galaxy Nexus", "Mounted at ~/Nexus", ("%s%sandroid_on.png", cwd, slash));

3番目のパラメーターは文字列であると想定されていますか?その場合は、snprintfを使用して個別にビルドする必要があります。

char path[1000];
snprintf (path, sizeof(path), "%s%sandroid_on.png", cwd, slash);
mount = notify_notification_new ("Samsung Galaxy Nexus", "Mounted at ~/Nexus", path);
fprintf(stdout, "Icon used %s\n", path);
于 2012-09-12T18:00:22.787 に答える
1
("%s%sandroid_on.png", cwd, slash)

C でのこの式が相当することを知っていますか?

(slash)

コンマ演算子には他に何の効果もありません!

次のようなことをしたいかもしれません:

char png[1100];
sprintf(png, "%s%sandroid_on.png", cwd, slash);
mount = notify_notification_new ("Samsung Galaxy Nexus", "Mounted at ~/Nexus", png);

または、特に char 配列をオーバーフローしないことがわかっている場合は、より簡単です。

strcat(cwd, slash);
strcat(cwd, "android_on.png");
mount = notify_notification_new ("Samsung Galaxy Nexus", "Mounted at ~/Nexus", cwd);
于 2012-09-12T17:56:05.937 に答える