2010-06-05 51 views
0

我想操作Stdin,然後Std*。但我收到以下錯誤,C:stdin和std * errs

$ 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工作的示例程序,相當失去了與程序執行如下所示的ERR報告

#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 

回答

2

你忘了你的main()函數!!

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

從事實且不說,你沒有main()功能,你的整個做法是錯誤的。 STDIN_FILENO是一個常數;分配給它沒有任何意義。

嘗試解釋你真正想要的,有一些細節,我們將能夠建議如何去做。

+0

是的,在我的回答中有很多我沒有得到,主要是因爲我對OP做了什麼感到困惑。 – amphetamachine 2010-06-07 15:03:08