【发布时间】:2016-04-16 08:44:11
【问题描述】:
我有一个非常简单的源读取文件描述符,它挂起。 有人能注意到代码的问题吗?
第一个是有问题的来源,第二个是在网络上找到的工作来源。两个来源几乎相同。
-
第一来源
#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); }
【问题讨论】:
-
究竟要做什么来测试这个?
-
我实现了复制需要读写的文件。为了澄清我的问题并让审阅者更容易看到,我删除了编写代码。
标签: c++ c filesystems system