我有一段代码(见下文)从作为命令行参数给出的文件中读取数据。我想添加对能够从管道读取输入的支持。例如,当前版本将数据读取为main <file_name>,而它也应该可以做一些 line cmd1 | main。这是从文件中读取数据的来源:
procedure main is
File : Ada.Text_IO.File_Type;
begin
if Ada.Command_Line.Argument_Count /= 1 then
return;
else
Ada.Text_IO.Open (
File => File,
Mode => In_File,
Name => Ada.Command_Line.Argument (1));
while (not Ada.Text_IO.End_Of_File (File)) loop
-- Read line using Ada.Text_IO.Get_Line
-- Process the line
end loop;
Ada.Text_IO.Close (File);
end main;
Run Code Online (Sandbox Code Playgroud)
如果我理解正确的话,管道只是 Ada 中的一种非常规文件类型。但是我该如何处理呢?
这些似乎都没有真正回答你的问题。管道只是使另一个程序的标准输出成为您程序的标准输入,因此您可以通过读取 Standard_Input 来读取管道。
函数 Current_Input 返回一个 File_Type。它最初返回 Standard_Input,但调用 Set_Input 会将其更改为返回您传递给 Set_Input 的任何内容。因此,如果没有给出文件,则如何从 Standard_Input 中读取,如果有,则从给定的文件中读取,大致如下所示:
File : File_Type;
if Argument_Count > 0 then
Open (File => File, Name => Argument (1), Mode => In_File);
Set_Input (File => File);
end if;
All_Lines : loop
exit All_Lines when End_Of_File (Current_Input);
Process (Line => Get_Line (Current_Input) );
end loop All_Lines;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
815 次 |
| 最近记录: |