gpt4 book ai didi

C: stdin 和 std* 错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:15:28 26 4
gpt4 key购买 nike

我想操纵 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

最佳答案

尝试使用 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* 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2982516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com