0

Stdin操作したいのですがStd*。しかし、次のエラーが発生します。

$ gcc testFd.c                                                                 
testFd.c:9: error: initializer element is not constant
testFd.c:9: warning: data definition has no type or storage class
testFd.c:10: error: redefinition of `fd'
testFd.c:9: error: `fd' previously defined here
testFd.c:10: error: `mode' undeclared here (not in a function)
testFd.c:10: error: initializer element is not constant
testFd.c:10: warning: data definition has no type or storage class
testFd.c:12: error: syntax error before string constant

プログラムを以下に示します。

#include <stdio.h>
#include <sys/ioctl.h>

int STDIN_FILENO = 1;
// I want to access typed 
// Shell commands, dunno about the value:
unsigned long F_DUPFD;

fd = fcntl(STDIN_FILENO, F_DUPFD, 0);
fd = open("/dev/fd/0", mode);

printf("STDIN = %s", fd);

更新されたエラー:ファイル記述子に関するサンプルプログラムをCで動作させるようにしようとしただけで、エラーレポートでかなり失われました

#include <stdio.h>
#include <sys/ioctl.h>

int main (void) {
    int STDIN_FILENO;
    // I want to access typed 
    // Shell commands, dunno about the value:
    unsigned long F_DUPFD;
    int fd;
    const char mode = 'r';

    fd = fcntl(STDIN_FILENO, F_DUPFD, 0);
    /* also, did you mean `fopen'? */
    fd = fopen("/dev/fd/0", mode);

    printf("STDIN = %s", fd);

    return 0;
}

プログラムの実行を以下に示します。

$ gcc testFd.c                                                                
testFd.c: In function `main':
testFd.c:14: warning: passing arg 2 of `fopen' makes pointer from integer without a cast
testFd.c:14: warning: assignment makes integer from pointer without a cast
4

4 に答える 4

3

mainメソッドを使用してみてください:

#include <stdio.h>
#include <sys/ioctl.h>

int main (void) {
    int STDIN_FILENO = 1;
    // I want to access typed 
    // Shell commands, dunno about the value:
    unsigned long F_DUPFD;
    /* also, declare the type of your variable "fd" */
    int fd;

    fd = fcntl(STDIN_FILENO, F_DUPFD, 0);
    /* also, did you mean `fopen'? */
    fd = open("/dev/fd/0", mode);

    printf("STDIN = %s", fd);

    return 0;
}
于 2010-06-05T23:28:38.847 に答える
2

あなたが機能を持っていないという事実は別としてmain()、あなたのアプローチ全体は間違っています。 STDIN_FILENO定数です。それに割り当てることは意味がありません。

あなたが実際にやりたいことを少し詳しく説明してみてください。そうすれば、その方法を提案することができます。

于 2010-06-06T02:02:34.613 に答える
2

main() 関数を忘れた!!

于 2010-06-05T23:27:17.893 に答える
2

の定義はどこにありmain()ますか?

于 2010-06-05T23:27:38.707 に答える