我有一个现有的程序,其中一条消息(例如,电子邮件或其他类型的消息)将进入stdin上的程序。 我知道stdin是一个FILE *,但我对它还有什么其他的特性有些困惑。我目前正试图在程序中添加一个检查,并且如果它包含一个特定的行(例如,单词“hello”),则会以不同的方式处理该消息。问题是,我需要搜索文件中的那个单词,但是我仍然需要stdin在程序中稍后指向它的原始位置。结构的概况如下:C文件指针,stdin上的多个读取
目前:
//actual message body is coming in on stdin
read_message(char type)
{
//checks and setup
if(type == 'm')
{
//when it reaches this point, nothing has touched stdin
open_and_read(); //it will read from stdin
}
//else, never open the message
}
我想添加其他检查,但在这里我要搜索邮件正文中。 像这样:
//actual message body is coming in on stdin
read_message(char type)
{
//checks and setup
//new check
if(message_contains_hello()) //some function that reads through the message looking for the word hello
{
other_functionality();
}
if(type == 'm')
{
//when it reaches this point, my new check may have modified stdin
open_and_read(); //it will read from stdin
}
//else, never open the message
}
的问题,这是搜索的邮件正文,我触摸到文件指针标准输入。但是,如果我仍然需要在第二个if语句中打开并阅读消息(如果type ='m'),则stdin需要指向它指向程序开始处的相同位置。我试图创建一个指针的副本,但只能成功创建一个副本,如果修改本身,它也会修改stdin。
我没有关于如何传递消息的选择 - 它必须留在stdin上。我如何访问标准输入框中的消息的实际主体而不修改标准输入自身? 基本上,我怎样才能读取它,然后让另一个函数能够从消息的开头读取呢?
好吧,喂。谢谢你的信息! – VMills
另一种可能性是使用'tmpfile'并将所有读取的数据存储到它提供的'FILE'中,然后倒带并从那里读取。 –
也unputc ... :) –