两个程序之间使用文件进行通信

发布于 2024-11-05 05:49:57 字数 1100 浏览 0 评论 0原文

我希望用 Pascal 编写的两个单独的程序使用附加文本文件在它们之间进行通信。它对于前 2-3 条消息工作正常,但随后它在 sender.pas 或 receiveer.pas 中给出运行时错误 你知道我的错误在哪里或者你有什么建议吗?

第一个程序receiver.pas

var
 f : text;
 s : string;
begin
 {I-}
 Assign(f,'main.in');
 while true do
  begin

   reset(f);
   while IOResult<>0 do      //
    begin                    // Wait until the file
     close(f);               //       is closed by sender
     reset(f);               //
    end;                     //

   if eof(f) then
    close(f)
   else
    begin
     readln(f,s);
     close(f);
     rewrite(f);
     close(f);
     writeln(s);
    end;
  end;
 {I+}
end.

第二个程序sender.pas

var
 f : text;
 s : string;
begin
 {I-}
 Assign(f,'main.in');
 while true do
  begin
   readln(s);

   rewrite(f);                //
   while IOResult<>0 do       //
    begin                     // Wait until the file
     close(f);                //        is closed by receiver
     rewrite(f);              //
    end;                      //

   writeln(f,s);
   close(f);
  end;
 {I+}
end.

I want two separate programs written in Pascal communicate between themselves using additional text file. It works fine for the first 2-3 messages but then it gives run-time error in either sender.pas or receiver.pas Do you know where my mistake is or do you have any suggestion?

First program receiver.pas

var
 f : text;
 s : string;
begin
 {I-}
 Assign(f,'main.in');
 while true do
  begin

   reset(f);
   while IOResult<>0 do      //
    begin                    // Wait until the file
     close(f);               //       is closed by sender
     reset(f);               //
    end;                     //

   if eof(f) then
    close(f)
   else
    begin
     readln(f,s);
     close(f);
     rewrite(f);
     close(f);
     writeln(s);
    end;
  end;
 {I+}
end.

second program sender.pas

var
 f : text;
 s : string;
begin
 {I-}
 Assign(f,'main.in');
 while true do
  begin
   readln(s);

   rewrite(f);                //
   while IOResult<>0 do       //
    begin                     // Wait until the file
     close(f);                //        is closed by receiver
     rewrite(f);              //
    end;                      //

   writeln(f,s);
   close(f);
  end;
 {I+}
end.

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

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

发布评论

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

评论(2

海拔太高太耀眼 2024-11-12 05:49:57

我注意到的一些事情:

  1. 如果重写失败,那么文件没有打开,您不必关闭它?关闭未打开的文件可能会导致运行时错误(尽管我假设分配会安全地初始化它),
  2. 具体取决于您如何使用它们,逻辑中可能存在错误。也就是说,关闭文件后可以直接供其他应用程序打开。一般来说,关闭的文件句柄会持续 100 毫秒到几秒钟(取决于文件系统业务)。这可能会导致此类方案中的饥饿问题。 (虽然它可以在普通的 dos 中工作,但不会停留,至少不会停留那么多)
  3. 如果文件不存在,读取器将崩溃。
  4. 我假设 {I-}/{I+} 是一个拼写错误,并且您的源代码确实读取 {$I-} 和 {$I+} (注意美元?)
  5. 在 Windows pascal 版本中,只读访问并不总是锁定。将 filemode:=2 作为所有内容的第一行。

这是什么编译器?德尔福、自由帕斯卡?哪个版本?

我的新(Free Pascal)接收器代码变为:

uses sysutils;
var
 f : text;
 s : string;
begin
 filemode := 2; // read-only
 {$I-}
 Assign(f,'main.in');
 while true do
  begin
   reset(f);
   while IOResult<>0 do      //
    begin                    // Wait until the file
     close(f);               //       is closed by sender
     sleep(1000);
     reset(f);
    end;
   if eof(f) then
    begin
      close(f);
      sleep(1000);
    end
   else
    begin
     readln(f,s);
     close(f);
     rewrite(f);
     close(f);
     writeln(s);
    end;
  end;
 {$I+}
end.

新的发送器代码是:

uses sysutils;
var
 f : text;
 s : string;
begin
 filemode := 2; // read-only
 {$I-}
 Assign(f,'main.in');
 while true do
  begin
   reset(f);
   while IOResult<>0 do      //
    begin                    // Wait until the file
     close(f);               //       is closed by sender
     sleep(1000);
     reset(f);
    end;
   if eof(f) then
    begin
      close(f);
      sleep(1000);
    end
   else
    begin
     readln(f,s);
     close(f);
     rewrite(f);
     close(f);
     writeln(s);
    end;
  end;
 {$I+}
end.

Some things I noticed:

  1. If rewrite fails, afaik the file was not opened and you don't have to close it? Closing an unopened file might cause runtime errors (though I assume assign will init it safely)
  2. depending on how you use these, there might be in the logic. Namely that after closing a file is directly ready for opening by other apps. In general closed filehandles linger several 100ms till several seconds (depending on filesystem busines). This can cause starvation problems in such schemes. (while it would work in plain dos, which didn't linger, at least not that much)
  3. The reader will crash if the file doesn't exist.
  4. I assume that the {I-}/{I+} is a typo and that your sourcecode really reads {$I-} and {$I+} (note the dollar?)
  5. In Windows pascal versions, read only access is not always locking. Put filemode:=2 as first line in everything.

What compiler is this? Delphi, Free Pascal? Which version?

My new (Free Pascal) receiver code becomes:

uses sysutils;
var
 f : text;
 s : string;
begin
 filemode := 2; // read-only
 {$I-}
 Assign(f,'main.in');
 while true do
  begin
   reset(f);
   while IOResult<>0 do      //
    begin                    // Wait until the file
     close(f);               //       is closed by sender
     sleep(1000);
     reset(f);
    end;
   if eof(f) then
    begin
      close(f);
      sleep(1000);
    end
   else
    begin
     readln(f,s);
     close(f);
     rewrite(f);
     close(f);
     writeln(s);
    end;
  end;
 {$I+}
end.

The new sender code is:

uses sysutils;
var
 f : text;
 s : string;
begin
 filemode := 2; // read-only
 {$I-}
 Assign(f,'main.in');
 while true do
  begin
   reset(f);
   while IOResult<>0 do      //
    begin                    // Wait until the file
     close(f);               //       is closed by sender
     sleep(1000);
     reset(f);
    end;
   if eof(f) then
    begin
      close(f);
      sleep(1000);
    end
   else
    begin
     readln(f,s);
     close(f);
     rewrite(f);
     close(f);
     writeln(s);
    end;
  end;
 {$I+}
end.
时光病人 2024-11-12 05:49:57

有几件事:确保在每次文件操作后检查 IOResult,而不仅仅是重写/重置 - 如果重置/重写失败,不要调用 close - 并且您可能需要在这些重试循环中使用诸如 Sleep(250) 之类的东西。

A couple of things: Make sure you check IOResult after every file operation, not just rewrite/reset - don't call close if your reset/rewrite failed - and you probably want something like a Sleep(250) in those retry loops.

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