C:stdin和std *错误 [英] C: stdin and std* errs

查看:61
本文介绍了C:stdin和std *错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要先操纵Stdin,然后再操纵Std*.但是我遇到了以下错误,

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

程序如下所示.

#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

推荐答案

尝试使用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;
}

这篇关于C:stdin和std *错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆