读取文件描述符挂起
read file descriptor HANGS
我有一个非常简单的源文件读取文件描述符,挂起。
有人能注意到代码的问题吗?
第一个是有问题的来源,第二个是在网上找到的有效来源。两个来源几乎相同。
第一个来源
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char ** argv) {
int n, in;
char buf[1024];
if ((in = open(argv[1], O_RDONLY)<0)) {
perror(argv[1]);
return -1;
}
while((n = read(in, buf, sizeof(buf))) > 0 ) { //HANGS at THIS LINE!!!!!!!!!!!
printf("TEST\n");
}
close(in);
return 0;
}
第二个工作来源来自网上
/*
* ============================================================================
* Name : sp_linux_copy.c
* Author : Marko Martinović
* Description : Copy input file into output file
* ============================================================================
**/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#define BUF_SIZE 8192
int main(int argc, char* argv[]) {
int input_fd; /* Input and output file descriptors */
ssize_t ret_in; /* Number of bytes returned by read() and write() */
char buffer[BUF_SIZE]; /* Character buffer */
/* Create input file descriptor */
input_fd = open (argv [1], O_RDONLY);
if (input_fd == -1) {
perror ("open");
return 2;
}
/* Copy process */
while((ret_in = read (input_fd, &buffer, BUF_SIZE)) > 0){
printf("TEST\n");
}
/* Close file descriptors */
close (input_fd);
}
一个有趣的巧合,您正在阅读 stdin
。这是因为在您的 if(in = ...
中您放错了一些括号。
发生的事情是首先对 open(argv[1], O_RDONLY)<0
求值,然后将结果放入 in
。由于 open()
的结果不小于零(在成功打开时),因此 in
变为 0。并且 stdin
是文件描述符的名称,它为零(在大多数系统上)。所以它是一个有效的文件描述符,read 很乐意从中读取。它只是没有得到任何东西,直到你在你的控制台中输入一些东西。
快速修复:
if ( (in = open(argv[1], O_RDONLY)) < 0) {
我有一个非常简单的源文件读取文件描述符,挂起。 有人能注意到代码的问题吗?
第一个是有问题的来源,第二个是在网上找到的有效来源。两个来源几乎相同。
第一个来源
#include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <stdio.h> int main(int argc, char ** argv) { int n, in; char buf[1024]; if ((in = open(argv[1], O_RDONLY)<0)) { perror(argv[1]); return -1; } while((n = read(in, buf, sizeof(buf))) > 0 ) { //HANGS at THIS LINE!!!!!!!!!!! printf("TEST\n"); } close(in); return 0; }
第二个工作来源来自网上
/* * ============================================================================ * Name : sp_linux_copy.c * Author : Marko Martinović * Description : Copy input file into output file * ============================================================================ **/ #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <errno.h> #include <sys/types.h> #include <unistd.h> #define BUF_SIZE 8192 int main(int argc, char* argv[]) { int input_fd; /* Input and output file descriptors */ ssize_t ret_in; /* Number of bytes returned by read() and write() */ char buffer[BUF_SIZE]; /* Character buffer */ /* Create input file descriptor */ input_fd = open (argv [1], O_RDONLY); if (input_fd == -1) { perror ("open"); return 2; } /* Copy process */ while((ret_in = read (input_fd, &buffer, BUF_SIZE)) > 0){ printf("TEST\n"); } /* Close file descriptors */ close (input_fd); }
一个有趣的巧合,您正在阅读 stdin
。这是因为在您的 if(in = ...
中您放错了一些括号。
发生的事情是首先对 open(argv[1], O_RDONLY)<0
求值,然后将结果放入 in
。由于 open()
的结果不小于零(在成功打开时),因此 in
变为 0。并且 stdin
是文件描述符的名称,它为零(在大多数系统上)。所以它是一个有效的文件描述符,read 很乐意从中读取。它只是没有得到任何东西,直到你在你的控制台中输入一些东西。
快速修复:
if ( (in = open(argv[1], O_RDONLY)) < 0) {