是否有一个标准函数可以打印/监视stdin文件的内容,同时将数据留在stdin中

Is there a standard-function which prints out/monitor the content of the stdin file while leaving the data inside stdin?

本文关键字:stdin 数据 文件 函数 标准 有一个 打印 是否 监视      更新时间:2023-10-16

标准库是否提供了一种更快的替代方案,只需读取stdin文件的所有内容/任何字符,将所有字符写入一个数组,然后将整个数组的内容分别交给stdoutstdin(因为其仅用于"分析"目的(?

我只想简单地分析stdin文件中的实际内容,而不是从中获取数据。

我找不到标准库的任何函数,它可以做到这一点。


我知道这很少,但我想我想要什么已经没有什么好说的了。

问题是针对C和C++的,因为我同时使用这两种语言。如果这两种语言之间的答案发生了变化,请说明重点是哪种语言。

您可以使用getchar按字符形式stdin读取字符。然而,在许多情况下,使用缓冲区(例如字符数组(会更快。

#include <stdio.h>
#include <stdlib.h>
int main(void) 
{ 
int ch;
while ((ch=getchar()) != EOF)   /* read/print "abcde" from stdin */
printf("%c", ch);
/* Test reason for reaching EOF. */
if (feof(stdin))          /* if failure caused by end-of-file condition */
puts("End of file reached");
else if (ferror(stdin))   /* if failure caused by some other error      */
{
perror("getchar()");
fprintf(stderr,"getchar() failed in file %s at line # %dn", __FILE__,__LINE__-9);
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}