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

时间:2021-01-31 14:04:28

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.

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

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

竞争通过将输出(使用管道,我猜想)重定向到文件并将其MD5与他们的匹配来评估输出....

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.

我的问题是,C中是否有任何东西,它不会写入输出流,但管道仍然得到它的输入。或者我甚至可能把问题弄错了。但不管怎样,我都在解决问题。

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...

我一直在优化算法。顺便说一下,他们接受许多人试图优化的makefile。对我来说,两个优化标志都没有奏效。我不知道还能做些什么......

2 个解决方案

#1


0  

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, ...);
  • 用int fd = fopen(“/ file / path”,O_WRONLY)打开文件; (您可能需要检查参数,自从我完成C编程以来已经很长时间了)然后写(fd,...);或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).
  • 用fopen打开文件,关闭标准输出并使用dup2()将文件描述符复制到文件描述符编号1(即标准输出)。

#2


0  

You may try fprintf on the pipe fd.

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

#1


0  

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, ...);
  • 用int fd = fopen(“/ file / path”,O_WRONLY)打开文件; (您可能需要检查参数,自从我完成C编程以来已经很长时间了)然后写(fd,...);或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).
  • 用fopen打开文件,关闭标准输出并使用dup2()将文件描述符复制到文件描述符编号1(即标准输出)。

#2


0  

You may try fprintf on the pipe fd.

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