从用C标准输入多次读取相同数据 [英] Reading the same data from stdin multiple times in C

查看:293
本文介绍了从用C标准输入多次读取相同数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写在C缓存模拟器,是基于跟​​踪文件,我想管进入通过标准输入程序。这些跟踪文件可达到15十亿线长,所以我不想将它们存储在活动内存的任何地方。我想为不同的存储器配置的模拟多次使用这是在输入到程序中指定的配置文件中的一个呼叫运行。该程序调用看起来应该是这样:

I'm writing a cache simulator in C that's based on trace files, which I want to pipe into the program via stdin. These trace files can be up to 15 billion lines long, so I don't want to store them anywhere in active memory. I want to run the simulation multiple times for different memory configurations from one call using a configuration file which is specified in the input to the program. The program call should look like this:

猫| (跟踪文件)./MemorySimulator -f(配置文件)

现在,在程序运行的方式是,它使用配置文件设置一个模拟参数,然后读取格式化数据的管道使用scanf()的,直到它到达跟踪文件的结束标准输入。然后,它前进到下一个配置从配置文件设置并尝​​试重新从跟踪文件中读取数据。这个过程一直持续到各种配置选项已经用尽。

Right now, the way the program runs is that it uses the config file to set the parameters of a simulation then reads the piped in formatted data from stdin using scanf() until it reaches the end of the trace file. It then proceeds to the next configuration setting from the config file and tries to read data from the trace file over again. This process continues until the various configuration options have been exhausted.

我遇到的问题是,一旦我通过跟踪文件运行一次,我无法再次捕获数据,从配置文件以下内存配置。

The problem I'm running into is that once I run through the trace file once, I'm unable to capture the data again for the following memory configuration from the config file.

有没有办法我的C程序中回收管道的数据,这样我可以从一个单一的程序执行多次运行模拟?到目前为止,我还没有能够找到一种方式来做到这一点。

Is there a way to recycle the pipe data within my C program so that I can run the simulation multiple times from a single program execution? So far, I haven't been able to find a way to accomplish this.

推荐答案

没有,那是行不通的。这是一个管道的本质。

No, that doesn't work. That's the very nature of a pipe.

可以没有该数据不被缓存的需求,并在它可以被重新请求在同一时间。

You cannot have the demand that data isn't cached and at the same time that it can be re-requested.

在一个管道,一个数据已经写入,就消失了,所以你haveto存储在某个地方,为了不迷路。

In a pipe, one the data has been written, it is gone, so you haveto store it somewhere in order not to get lost.

你可以做到这一点的唯一方法就是模仿其他程序的行为 - 这应该是在猫琐碎情况

The only way you can accomplish this is to "imitate" the behaviour of the other program - which should be trivial in the cat case.

要确切的说,你的code是著名的UUOC一个很好的例子(Unneecessary使用)。

To be exact, your code is a very good example for the famous UUOC (Unneecessary Use of cat).

如果您被要求从标准输入读取 - 很好,那还没有成为一个管道。而不是

If you are requested to read from stdin - well, that hasn't to be a pipe. Instead of

cat file | program

您可以做

program < file

和这不给你一个管道,而是直接访问文件,包括寻求的能力。

and this doesn't give you a pipe, but direct access to the file, including the ability to seek.

您可以如果可能利用这一点,如果不是,用户可以自己缓存数据或拒绝运行

You could use this if possible, and if not, either cache the data yourself or refuse to run.

然而,这不,如果你被要求接受工作的所有的各类标准输入。

This, however, doesn't work if you are requested to accept all kinds of standard input.

这篇关于从用C标准输入多次读取相同数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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