第二次打开stdin [英] open stdin a second time

查看:96
本文介绍了第二次打开stdin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我打开stdin来获取文件并将其传递给管道。


razor = popen(" ; / var / qmail / bin / razor-check -home = / var / qmail / razor",

" w");

while((ret = fread (linha,1,sizeof(linha),stdin))0){

fwrite(linha,1,sizeof(linha),razor);

}

pclose(剃须刀)


之后,如果剃刀给我一个错误,我想发送一封电子邮件,附上原始邮件

。 ..但是我怎样才能重读stdin来做

那个。


我不想将消息保存到临时文件或保存到内存

用于i / o问题。有没有办法重读stdin?


Fernando

解决方案

fbertasso写道:





我打开stdin获取文件并通过管道传递它。

razor = popen(" / var / qmail / bin / razor-check -home = / var / qmail / razor",

" w");

while((ret = fread(linha,1,sizeof(linha),stdin))0){

fwrite(linha,1,sizeof(linha),razor);

}

pclose(razor)


之后,如果razor给我一个错误,我想发送一封电子邮件

附上原始邮件......但是我怎样才能重读stdin来做

那个。


我不想保存消息到临时文件或将其保存到内存

用于i / o问题。有没有办法重新读取标准输入?



你*可能*能够在第一次读取之前记录stdin流的起始位置

,并且

返回该位置再次阅读:


fpos_t where;

if(fgetpos(stdin,& where)!= 0 ){

perror(无法确定位置);

...

}

...从stdin读取数据...

if(want_to_read_again){

if(fgetpos_failed_above){

fputs("对不起) ,查理!\ n",stderr);

...

}

else if(fsetpos(stdin,& where) != 0){

perror(无法恢复位置);

...

}

...第二次读stdin ...

}


这是否有效取决于什么样的

源stdin流读取。像磁盘文件

这样的东西通常是可重新定位的,而键盘和

插座和管道之类的东西通常都没有。


确保数据可用的唯一方法

第二次看是在第一次阅读它时将它保存在某个地方




-
呃********* @ sun.com


fbertasso写道:


>



.... snip ...


>

我不想将消息保存到临时文件或将其保存到内存以用于i / o问题。有没有办法重新读取标准输入?



否。您不知道在stdin上生成了什么输入。它可能是键盘上的一只猴子。它可能是一个闪电

螺栓。


-

[邮件]:Chuck F(cbfalconer at maineline dot net)

[page]:< http://cbfalconer.home.att.net>

尝试下载部分。

* *发布于 http://www.teranews.com **


6月9日下午6:46,fbertasso< fberta ... @ gmail.comwrote:





我打开stdin来获取文件并通过管道传递它。


razor = popen(" / var / qmail / bin / razor-check -home = / var / qmail / razor",

" w");

while((ret = fread(linha,1,sizeof( linha),stdin))0){

fwrite(linha,1,sizeof(linha),razor);}


pclose(razor)


之后,如果razor给我一个错误,我想发送一封电子邮件,附上原始邮件

但是我该如何重新发送ad stdin要做什么

那个。


我不想将消息保存到临时文件或将其保存到内存中

用于i / o问题。有没有办法重读stdin?


费尔南多



当然fgetpos和fsetpos或其他寻求技巧不是

开始工作

如果您从键盘输入。如果您正在阅读

a文件

,那么ftell / fseek或fgetpos / fsetpos就是一次性的。


给其他人:

除了磁盘文件之外,是否还有其他流可以重新定位

的感觉(在

寻求感)?


Hi,

I′m opening stdin to get a file and pass it through a pipe.

razor=popen ("/var/qmail/bin/razor-check -home=/var/qmail/razor",
"w");
while( (ret=fread(linha,1,sizeof(linha),stdin) ) 0 ) {
fwrite(linha,1,sizeof(linha),razor);
}
pclose(razor)

After that, if razor returns me an error I want to send an email with
the original message attached...but how can I re-read stdin to do
that.

I do not want to save the message to a temp file or save it to memory
for i/o questions. Is there a way to re-read stdin ?

Fernando

解决方案

fbertasso wrote:

Hi,

I′m opening stdin to get a file and pass it through a pipe.

razor=popen ("/var/qmail/bin/razor-check -home=/var/qmail/razor",
"w");
while( (ret=fread(linha,1,sizeof(linha),stdin) ) 0 ) {
fwrite(linha,1,sizeof(linha),razor);
}
pclose(razor)

After that, if razor returns me an error I want to send an email with
the original message attached...but how can I re-read stdin to do
that.

I do not want to save the message to a temp file or save it to memory
for i/o questions. Is there a way to re-read stdin ?

You *might* be able to record the starting position of
the stdin stream before reading it the first time, and
return to that position to read it again:

fpos_t where;
if (fgetpos(stdin, &where) != 0) {
perror("Can''t determine position");
...
}
... read data from stdin ...
if (want_to_read_again) {
if (fgetpos_failed_above) {
fputs("Sorry, Charlie!\n", stderr);
...
}
else if (fsetpos(stdin, &where) != 0) {
perror("Can''t restore position");
...
}
... read stdin a second time ...
}

Whether this works or not depends greatly on what kind of
source the stdin stream reads from. Things like disk files
are often "repositionable," while things like keyboards and
sockets and pipes usually are not.

The only way to be certain of having the data available
for a second look is to save it somewhere while reading it
the first time.

--
Er*********@sun.com


fbertasso wrote:

>

.... snip ...

>
I do not want to save the message to a temp file or save it to
memory for i/o questions. Is there a way to re-read stdin ?

No. You have no idea what generated the input on stdin. It might
have been a monkey on the keyboard. It might have been a lightning
bolt somewhere.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **


On Jun 9, 6:46 pm, fbertasso <fberta...@gmail.comwrote:

Hi,

I′m opening stdin to get a file and pass it through a pipe.

razor=popen ("/var/qmail/bin/razor-check -home=/var/qmail/razor",
"w");
while( (ret=fread(linha,1,sizeof(linha),stdin) ) 0 ) {
fwrite(linha,1,sizeof(linha),razor);}

pclose(razor)

After that, if razor returns me an error I want to send an email with
the original message attached...but how can I re-read stdin to do
that.

I do not want to save the message to a temp file or save it to memory
for i/o questions. Is there a way to re-read stdin ?

Fernando

Of course fgetpos and fsetpos or other seeking techniques are not
going to work
if you are taking the input from the keyboard. If you are reading from
a file
then ftell/fseek or fgetpos/fsetpos are at you disposable.

To everyone else:
Apart from disk files, is there any other stream which can be
repositioned ( in the
sense of seeking)?


这篇关于第二次打开stdin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆