为什么 scanf 可以读取超过 1024 个字符,而标准输入流缓冲区只有 1024 个字节?
Why scanf can read more than 1024 character while stdin stream buffer is 1024 bytes only?
我可以通过 APUE:[=18= 中的代码获取 stdin
连接我的终端的流缓冲区大小和缓冲区类型 Ubuntu ]
#include "apue.h" //It's merely a wrapper header
void pr_stdio(const char *, FILE *);
int is_unbuffered(FILE *);
int is_linebuffered(FILE *);
int buffer_size(FILE *);
int
main(void)
{
FILE *fp;
fputs("enter any character\n", stdout);
if (getchar() == EOF)
err_sys("getchar error");
fputs("one line to standard error\n", stderr);
pr_stdio("stdin", stdin);
pr_stdio("stdout", stdout);
pr_stdio("stderr", stderr);
if ((fp = fopen("/etc/passwd", "r")) == NULL)
err_sys("fopen error");
if (getc(fp) == EOF)
err_sys("getc error");
pr_stdio("/etc/passwd", fp);
exit(0);
}
void
pr_stdio(const char *name, FILE *fp)
{
printf("stream = %s, ", name);
if (is_unbuffered(fp))
printf("unbuffered");
else if (is_linebuffered(fp))
printf("line buffered");
else /* if neither of above */
printf("fully buffered");
printf(", buffer size = %d\n", buffer_size(fp));
}
int
is_unbuffered(FILE *fp)
{
return(fp->_flags & _IO_UNBUFFERED);
}
int
is_linebuffered(FILE *fp)
{
return(fp->_flags & _IO_LINE_BUF);
}
int
buffer_size(FILE *fp)
{
return(fp->_IO_buf_end - fp->_IO_buf_base);
}
运行 我从终端得到的上面的代码:stream = stdin, line buffered, buffer size = 1024
.
那我写个测试:
#include "stdio.h"
int main(){
char c[2048];
c[1033] = 'a';
scanf("%s", c); //I paste 1440 '1' into the terminal.
printf("%c", c[1033]); //I expect it to be 'a'.
return 0;
}
我将 1440(>1024 字节)字符“1”粘贴到终端,并期望多余的输入数据会以某种方式被丢弃,因为行缓冲区大小仅为 1024 字节。但最终,我用 c[1033]
.
打印了 '1'
为什么scanf
可以读取超过1024 个字符,而stdin
的缓冲区大小只有1024B?
引自APUE:
We can see that the default for this system is to have standard input and standard
output line buffered when they’re connected to a terminal. The line buffer is 1,024
bytes. Note that this doesn’t restrict us to 1,024-byte input and output lines; that’s just
the size of the buffer. Writing a 2,048-byte line to standard output will require two
write system calls.
或者我应该问怎么办?
我不太理解强调的文字。缓冲区不限制用户输入的大小吗?为什么它说我可以输入超过缓冲区大小的数据(每个输入 )?谁能告诉我它是如何工作的?
如评论中所述,当 scanf()
到达第一个缓冲区已满的末尾时,如果它仍然需要更多数据,它会返回系统以获取更多数据,可能多次。缓冲区只是一种方便和优化措施。
我可以通过 APUE:[=18= 中的代码获取 stdin
连接我的终端的流缓冲区大小和缓冲区类型 Ubuntu ]
#include "apue.h" //It's merely a wrapper header
void pr_stdio(const char *, FILE *);
int is_unbuffered(FILE *);
int is_linebuffered(FILE *);
int buffer_size(FILE *);
int
main(void)
{
FILE *fp;
fputs("enter any character\n", stdout);
if (getchar() == EOF)
err_sys("getchar error");
fputs("one line to standard error\n", stderr);
pr_stdio("stdin", stdin);
pr_stdio("stdout", stdout);
pr_stdio("stderr", stderr);
if ((fp = fopen("/etc/passwd", "r")) == NULL)
err_sys("fopen error");
if (getc(fp) == EOF)
err_sys("getc error");
pr_stdio("/etc/passwd", fp);
exit(0);
}
void
pr_stdio(const char *name, FILE *fp)
{
printf("stream = %s, ", name);
if (is_unbuffered(fp))
printf("unbuffered");
else if (is_linebuffered(fp))
printf("line buffered");
else /* if neither of above */
printf("fully buffered");
printf(", buffer size = %d\n", buffer_size(fp));
}
int
is_unbuffered(FILE *fp)
{
return(fp->_flags & _IO_UNBUFFERED);
}
int
is_linebuffered(FILE *fp)
{
return(fp->_flags & _IO_LINE_BUF);
}
int
buffer_size(FILE *fp)
{
return(fp->_IO_buf_end - fp->_IO_buf_base);
}
运行 我从终端得到的上面的代码:stream = stdin, line buffered, buffer size = 1024
.
那我写个测试:
#include "stdio.h"
int main(){
char c[2048];
c[1033] = 'a';
scanf("%s", c); //I paste 1440 '1' into the terminal.
printf("%c", c[1033]); //I expect it to be 'a'.
return 0;
}
我将 1440(>1024 字节)字符“1”粘贴到终端,并期望多余的输入数据会以某种方式被丢弃,因为行缓冲区大小仅为 1024 字节。但最终,我用 c[1033]
.
为什么scanf
可以读取超过1024 个字符,而stdin
的缓冲区大小只有1024B?
引自APUE:
We can see that the default for this system is to have standard input and standard output line buffered when they’re connected to a terminal. The line buffer is 1,024 bytes. Note that this doesn’t restrict us to 1,024-byte input and output lines; that’s just the size of the buffer. Writing a 2,048-byte line to standard output will require two write system calls.
或者我应该问怎么办?
我不太理解强调的文字。缓冲区不限制用户输入的大小吗?为什么它说我可以输入超过缓冲区大小的数据(每个输入 )?谁能告诉我它是如何工作的?
如评论中所述,当 scanf()
到达第一个缓冲区已满的末尾时,如果它仍然需要更多数据,它会返回系统以获取更多数据,可能多次。缓冲区只是一种方便和优化措施。