程序输出可以通过程序本身重定向到管道吗?

发布于 2024-12-05 16:32:59 字数 373 浏览 1 评论 0原文

嗯,这是关于比赛的节目。

我正在提交一个程序&发现我的指标在总执行速度方面比得分最高的人要慢得多。所有其他的(页面错误、内存......)都是相似的。我发现当我在没有 printf (或 write)的情况下运行我的程序时,我的总执行速度(在我自己的电脑上测量)似乎相似。

竞赛通过将输出(我想是用管道)重定向到文件和文件中来评估输出。将其 MD5 与他们的相匹配...

我的问题是,C 中是否有某种东西不会写入输出流,但管道仍然会获取其输入。或者也许我什至错误地提出了这个问题。但无论如何,我都陷入了困境。

我一直在努力优化算法。顺便说一句,他们接受许多人试图优化的 makefile。对我来说,这两个优化标志都不起作用。我也不知道对此还能做什么......

Well this is regarding a program for a competition.

I was submitting a program & finding my metrics to be relatively way slower than the top scorers in terms of total execution speed. All others (page faults, memory...) were similar. I found that when I ran through my program without the printf (or write) my total execution speed (as measured in my own pc) seemed to be similar.

The competition evaluates the output by redirecting the output (with a pipe, i suppose) into a file & matching its MD5 with theirs....

My question is, Is there by any means something in C, that doesn't write to the output stream but still the pipe gets its input. Or perhaps I am even framing the question wrong. But either way, I am in a fix.

I have been beating my head off with optimizing the algorithm. BTW they accept makefile where many have tried to optimize. For me neither of the optimization flags have worked. I don't know what else can be done about that too...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡墨 2024-12-12 16:32:59

如果您需要编写一个将其输出写入文件的程序,您只需:

  • 使用 int fd = fopen("/file/path", O_WRONLY); 打开文件(您可能需要检查参数,已经很久没有做过C编程了)然后write(fd, ...);或者fprintf(fd, ...);< /code>
  • 使用 fopen 打开文件,关闭标准输出并使用 dup2() 将文件描述符复制到文件描述符编号 1(即标准输出)。

If you need to make a program that writes its output to a file, you just need to:

  • open the file with int fd = fopen("/file/path", O_WRONLY); (you may need to check the parameters, it's been a long time since I've done C programming) and then write(fd, ...); or fprintf(fd, ...);
  • open the file with fopen, close the standard output and use dup2() to duplicate the file descriptor to the file descriptor number 1 (i.e. standard output).
硪扪都還晓 2024-12-12 16:32:59

您可以在管道 fd 上尝试 fprintf 。

You may try fprintf on the pipe fd.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文