写入 Fortran 和 C 程序之间的管道时出现问题

发布于 2024-10-30 16:39:14 字数 525 浏览 0 评论 0 原文

我有一个由其他人用 Fortran 语言编写的程序,它会从标准输入中读取一些内容,然后进行一些计算并输出结果。我想做的是使用来自我用 C 编写的另一个程序的不同输入数据多次运行它。为此,我使用 popen

  FILE *pipe = popen(".\\program.exe", "wt");
  if (!pipe) {
    exit(1);
  }

  fprintf(pipe, "%d\n", thing1);
  fprintf(pipe, "%d\n", thing2);
  ...

  pclose(pipe);

问题是它不能以这种方式工作。它与“program.exe < input.txt”完美配合,但不是这样。它读取第一件事,然后输出这个愚蠢的错误:“IO-09系统文件错误 - 未知错误”。当然,我不知道这意味着什么,因为我从未编写过 Fortran 程序。

我做错了什么?

编辑:

不幸的是我没有该程序的源代码

I have a program written in Fortran by someone else which consequently reads a few things from the standard input and then does some calculations and outputs the result. What I want to do is to run it many times with different input data from another program, written in C by me. To do this I use popen:

  FILE *pipe = popen(".\\program.exe", "wt");
  if (!pipe) {
    exit(1);
  }

  fprintf(pipe, "%d\n", thing1);
  fprintf(pipe, "%d\n", thing2);
  ...

  pclose(pipe);

The problem is that it doesn't work this way. It works perfectly with "program.exe < input.txt" but not this way. It reads the first thing and then outputs this stupid error: "IO-09 system file error - unknown error". Of course I have no idea what this means as I've never programmed Fortran.

What am I doing wrong?

EDIT:

Unfortunately I have no source code of that program

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

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

发布评论

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

评论(2

枕花眠 2024-11-06 16:39:14

看起来应该可以,但不知道为什么不行。你确定吗
popen() 在您的 Windows 计算机上可用吗?我依稀记得
不适用于某些 Windows 系统。你可以尝试一下
简单的 C 程序,看看它是 popen() 还是 Fortran 程序。

作为解决方法,您可以将数据写入临时文件,然后
使用 system(".\\program.exe 调用 Fortran 程序。
是的,这是一个拼凑。

It looks like it should work, not sure why it doesn't. Are you sure
popen() is available on your Windows machine? I vaguely recall it
not being available for some Windows systems. You could try it with a
simple C program and see if it's popen() or the Fortran program.

As a workaround, you could write your data to a temporary file, then
use system(".\\program.exe < tempfile") to call the Fortran program.
Yeah, it's a kludge.

浪荡不羁 2024-11-06 16:39:14

您没有说明您正在使用哪个 Fortran 编译器,但最近修复了 gfortran 中长期存在的错误,该错误导致从管道读取失败。请参阅

http://gcc.gnu.org/bugzilla/show_bug.cgi?id= 47694

因此,如果您使用的是 gfortran,您可能需要尝试更新到具有错误修复的版本。

You don't say which Fortran compiler you're using, but recently a long-standing bug in gfortran was fixed where reading from a pipe failed. See

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47694

So in case you're using gfortran you might want to try to update to a version that has the bugfix.

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