C: ошибки стандартного ввода и стандартного ввода*

Я хочу манипулировать 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

person otto    schedule 05.06.2010    source источник


Ответы (4)


Попробуйте использовать метод 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;
}
person amphetamachine    schedule 05.06.2010

Вы забыли свою функцию main()!!

person Artelius    schedule 05.06.2010

Где ваше определение main()?

person crazyscot    schedule 05.06.2010

Помимо того факта, что у вас нет функции main(), весь ваш подход неверен. STDIN_FILENO — константа; назначать его нет никакого смысла.

Попробуйте объяснить, что вы на самом деле хотите сделать, с некоторыми подробностями, и мы сможем предложить, как это сделать.

person caf    schedule 06.06.2010
comment
Да, я многого не понял в своем ответе, в основном потому, что я был сбит с толку тем, что пытался сделать ОП. - person amphetamachine; 07.06.2010