C: stdin and std* errs

帅比萌擦擦* 提交于 2019-12-25 00:37:27

问题


I want to manipulate Stdin and then Std*. But I am getting the following errors,

$ 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

The program is shown below.

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

Updated Errors: just trying to get an example program about file descriptors to work in C, pretty lost with the err report

#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;
}

The program execution is shown below.

$ 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

回答1:


Try using a main method:

#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;
}



回答2:


You forgot your main() function!!




回答3:


Where's your definition of main()?




回答4:


Quite apart from the fact that you don't have a main() function, your entire approach is wrong. STDIN_FILENO is a constant; assigning to it doesn't make any sense.

Try explaining what you actually want to do, with some detail, and we will be able to suggest how to go about it.



来源:https://stackoverflow.com/questions/2982516/c-stdin-and-std-errs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!