英文:
read from stdin, and write to stdout, why my read() function alwasy return 1?
问题
我有一个简单的代码来测试read()和write()系统调用,如下所示:
```c
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
int main()
{
#define BUFFSIZE 4096
int n;
char buf[BUFFSIZE];
while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) {
if (write(STDOUT_FILENO, buf, n) != n) {
printf("write err.\n");
}
}
if (n < 0) {
printf("read error.\n");
}
return 0;
}
我像这样运行它:
$ ./a.out
hello
h
我使用gdb并发现read()始终返回1。
<details>
<summary>英文:</summary>
I have a simple code to test read() and write() system call, as below
```c
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
int main()
{
#define BUFFSIZE 4096
int n;
char buf[BUFFSIZE];
while (n=read(STDIN_FILENO, buf, BUFFSIZE) > 0) {
if (write(STDOUT_FILENO, buf, n) != n) {
printf("write err.\n");
}
}
if (n < 0) {
printf("read error.\n");
}
return 0;
}
and I run it like this:
$ ./a.out
hello
h
I use gdb and find read() always returns 1.
答案1
得分: 3
The statement:
while (n=read(STDIN_FILENO, buf, BUFFSIZE) > 0)
is parsed as:
while ( n = (read (STDIN_FILENO, buf, BUFSIZE) > 0))
The comparison operator >
has higher precedence than the assignment operator =
. Comparison and logical operators return the value false
or true
, which in this statement gets stored in the variable n
. You need parentheses around the assignment to n
in order to secure the order of operations like so:
while ( (n = read (STDIN_FILENO, buf, BUFSIZE)) > 0)
Alternatively, the loop can be rewritten in this manner:
for (;;) {
n = read (STDIN_FILENO, buf, BUFSIZE);
if (n <= 0) {
break;
}
if (write (STDOUT_FILENO, buf, n) != n) {
fputs ("write error.\n", stderr);
}
}
Note that read()
returns a ssize_t
, not an int
.
英文:
The statement:
while (n=read(STDIN_FILENO, buf, BUFFSIZE) > 0)
is parsed as:
while ( n = (read (STDIN_FILENO, buf, BUFSIZE) > 0))
The comparison operator >
has higher precedence than the assignment operator =
. Comparison and logical operators return the value false
or true
, which in this statement gets stored in the variable n
. You need parentheses around the assignment to n
in order to secure order of operations like so:
while ( (n = read (STDIN_FILENO, buf, BUFSIZE)) > 0)
Alternatively, the loop can be rewritten in this manner:
for (;;) {
n = read (STDIN_FILENO, buf, BUFSIZE);
if (n <= 0) {
break;
}
if (write (STDOUT_FILENO, buf, n) != n) {
fputs ("write error.\n", stderr);
}
}
Note that read()
returns a ssize_t
, not an int
.
答案2
得分: 2
你必须在while条件中加上括号,否则 n
是 read(STDIN_FILENO, buf, BUFFSIZE)) > 0
的结果。
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
#define BUFFSIZE 4096
int n;
char buf[BUFFSIZE];
while ((n=read(STDIN_FILENO, buf, BUFFSIZE)) > 0) {
if (write(STDOUT_FILENO, buf, n) != n) {
printf("write err.\n");
}
}
if (n < 0) {
printf("read error.\n");
}
return 0;
}
英文:
You must put parentheses in the while condition, otherwise n
is the result of read(STDIN_FILENO, buf, BUFFSIZE)) > 0
.
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
int main()
{
#define BUFFSIZE 4096
int n;
char buf[BUFFSIZE];
while ((n=read(STDIN_FILENO, buf, BUFFSIZE)) > 0) {
if (write(STDOUT_FILENO, buf, n) != n) {
printf("write err.\n");
}
}
if (n < 0) {
printf("read error.\n");
}
return 0;
}
答案3
得分: 0
高级问题
节省时间。启用所有警告。比在SO上发帖更有效。
while (n=read(STDIN_FILENO, buf, BUFFSIZE) > 0) {
警告:建议在用作真值的赋值周围加上括号[-Wparentheses]
以及其他。
英文:
Higher level problem
Save time. Enable all warnings. More efficient than posting on SO.
while (n=read(STDIN_FILENO, buf, BUFFSIZE) > 0) {
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
and others.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论