* Newbie Question: using Text_IO.Open() to read from standard input
@ 2005-10-13 0:49 pamela.lum
2005-10-13 1:30 ` Anh Vo
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: pamela.lum @ 2005-10-13 0:49 UTC (permalink / raw)
Hi,
I need to open a file that is passed to my ada executable as standard
input, in the following fashion:
./myprogram < inputfile.txt
My question is: How can i get the Open() function to open a file from
standard input so that I can bypass using a static file name? For
example:
Integer_File : FILE_TYPE;
Open(Integer_File, In_File, "inputfile.txt"); <-- don't open
inputfile.txt, open file from standard input instead
I know that this can be done easily with Ada.Text_IO.Text_Streams,
however I am stubborn and curious to see if there is a simple and easy
way to do it with the Open() function.
THanks
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Newbie Question: using Text_IO.Open() to read from standard input
2005-10-13 0:49 Newbie Question: using Text_IO.Open() to read from standard input pamela.lum
@ 2005-10-13 1:30 ` Anh Vo
2005-10-13 1:48 ` pamela.lum
2005-10-13 1:58 ` tmoran
2005-10-13 3:15 ` Steve
2 siblings, 1 reply; 11+ messages in thread
From: Anh Vo @ 2005-10-13 1:30 UTC (permalink / raw)
Just use predefined Ada.Command_Line package. The function Argument (1)
should give you the name of the input file (inputfile.txt). Then, you
can open it as mentioned above.
AV
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Newbie Question: using Text_IO.Open() to read from standard input
2005-10-13 1:30 ` Anh Vo
@ 2005-10-13 1:48 ` pamela.lum
2005-10-13 1:56 ` Anh Vo
2005-10-13 16:14 ` Martin Krischik
0 siblings, 2 replies; 11+ messages in thread
From: pamela.lum @ 2005-10-13 1:48 UTC (permalink / raw)
Tried that (see the code below named test_cl.ada). It fails to detect
arguments after the redirect ("<" ) character in the command :
./myprogram < inputfile.txt
For example, the output of the code below (test_cl.ada) is as follows:
Input:
./test_cl < inputfile.txt
The command name : ./test_cl
The number of arguments: 0 <-- does not recognize anything after "<" as
an argument
So why don't i drop the redirect character? Because this is an
assignment and our code will be tested using the redirect.
Suggestions?
CODE FOR DISPLAYING COMMAND LINE ARGUMENTS:
Filename: test_cl.ada
with Ada.Command_Line;
with Gnat.Io; use Gnat.Io;
procedure Test_CL is
begin
-- Writes out the command name (argv[0])
Put (" The command name : ");
Put (Ada.Command_Line.Command_Name);
New_Line;
-- Writes out the number of arguments passed to the program (argc)
Put ("The number of arguments: ");
Put (Ada.Command_Line.Argument_Count);
New_Line;
-- Writes out all the arguments using the Argument function.
-- (BE CAREFUL because if the number you pass to Argument is not
-- in the range 1 .. Argument_Count you will get
Constraint_Error!)
for I in 1 .. Ada.Command_Line.Argument_Count loop
Put (" Argument ");
Put (I);
Put (": ");
Put (Ada.Command_Line.Argument (I));
New_Line;
end loop;
end Test_CL;
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Newbie Question: using Text_IO.Open() to read from standard input
2005-10-13 1:48 ` pamela.lum
@ 2005-10-13 1:56 ` Anh Vo
2005-10-13 16:14 ` Martin Krischik
1 sibling, 0 replies; 11+ messages in thread
From: Anh Vo @ 2005-10-13 1:56 UTC (permalink / raw)
I assumed you took out the < sign. That means < sign is unnecessary.
Command line treats it as the first argument, the file name as second.
I would recommend run it like this "test_cl inputfile.txt". It should
work. I am absolutely sure of it.
AV
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Newbie Question: using Text_IO.Open() to read from standard input
2005-10-13 0:49 Newbie Question: using Text_IO.Open() to read from standard input pamela.lum
2005-10-13 1:30 ` Anh Vo
@ 2005-10-13 1:58 ` tmoran
2005-10-13 2:02 ` pamela.lum
2005-10-13 5:26 ` Jeffrey R. Carter
2005-10-13 3:15 ` Steve
2 siblings, 2 replies; 11+ messages in thread
From: tmoran @ 2005-10-13 1:58 UTC (permalink / raw)
> I need to open a file that is passed to my ada executable as standard
> input, in the following fashion:
>
> ./myprogram < inputfile.txt
>
> My question is: How can i get the Open() function to open a file from
> standard input so that I can bypass using a static file name?
Standard Input is already Open when your program starts, so you
don't need to open it.
ARM A.10(4): "For all Get and Put procedures that operate on text
files, and for many other subprograms, there are forms with and without a
file parameter. ... If no file is specified, a default input file or a
default output file is used." That's simplest, but you might want instead
to use things from ARM 10.3 "Default Input, Output, and Error Files".
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Newbie Question: using Text_IO.Open() to read from standard input
2005-10-13 1:58 ` tmoran
@ 2005-10-13 2:02 ` pamela.lum
2005-10-13 2:19 ` Anh Vo
2005-10-13 5:26 ` Jeffrey R. Carter
1 sibling, 1 reply; 11+ messages in thread
From: pamela.lum @ 2005-10-13 2:02 UTC (permalink / raw)
Heehee...
"Standard Input is already Open when your program starts, so you
don't need to open it. "
Just read that right before you posted.
Thanks all!
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Newbie Question: using Text_IO.Open() to read from standard input
2005-10-13 2:02 ` pamela.lum
@ 2005-10-13 2:19 ` Anh Vo
0 siblings, 0 replies; 11+ messages in thread
From: Anh Vo @ 2005-10-13 2:19 UTC (permalink / raw)
I would like to know if your program worked as discussed? Can you
confirm that?
AV
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Newbie Question: using Text_IO.Open() to read from standard input
2005-10-13 0:49 Newbie Question: using Text_IO.Open() to read from standard input pamela.lum
2005-10-13 1:30 ` Anh Vo
2005-10-13 1:58 ` tmoran
@ 2005-10-13 3:15 ` Steve
2 siblings, 0 replies; 11+ messages in thread
From: Steve @ 2005-10-13 3:15 UTC (permalink / raw)
<pamela.lum@gmail.com> wrote in message
news:1129164557.586640.154480@g14g2000cwa.googlegroups.com...
> Hi,
>
> I need to open a file that is passed to my ada executable as standard
> input, in the following fashion:
>
> ./myprogram < inputfile.txt
>
> My question is: How can i get the Open() function to open a file from
> standard input so that I can bypass using a static file name? For
> example:
>
> Integer_File : FILE_TYPE;
> Open(Integer_File, In_File, "inputfile.txt"); <-- don't open
> inputfile.txt, open file from standard input instead
>
> I know that this can be done easily with Ada.Text_IO.Text_Streams,
> however I am stubborn and curious to see if there is a simple and easy
> way to do it with the Open() function.
>
> THanks
>
The following simple example works on red hat linux 8.0
with Ada.Text_IO;
procedure TestPassThru is
package Text_IO renames Ada.Text_IO;
begin
while not Text_IO.End_Of_File loop
declare
text : String( 1 .. 255 );
last : Natural;
begin
Text_IO.Get_Line( text, last );
Text_IO.Put_Line( text( 1 .. last ) );
end;
end loop;
end TestPassThru;
(Sorry for any typo's I had to copy the code from one screen to another).
Steve
(The Duck)
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Newbie Question: using Text_IO.Open() to read from standard input
2005-10-13 1:58 ` tmoran
2005-10-13 2:02 ` pamela.lum
@ 2005-10-13 5:26 ` Jeffrey R. Carter
2005-10-13 6:53 ` tmoran
1 sibling, 1 reply; 11+ messages in thread
From: Jeffrey R. Carter @ 2005-10-13 5:26 UTC (permalink / raw)
tmoran@acm.org wrote:
> Standard Input is already Open when your program starts, so you
> don't need to open it.
> ARM A.10(4): "For all Get and Put procedures that operate on text
> files, and for many other subprograms, there are forms with and without a
> file parameter. ... If no file is specified, a default input file or a
> default output file is used." That's simplest, but you might want instead
> to use things from ARM 10.3 "Default Input, Output, and Error Files".
To clarify a little further, you shouldn't open or close standard input. The Get
and Get_Line procedures that don't take a file parameter read from
Current_Input. Current_Input is Standard_Input by default. (There's also a
Current_Output for Put and Put_Line that default to Standard_Output.)
These procedures read from Current_Input rather than Standard_Input to allow a
program to read from a file if one if provided and from Standard_Input if not:
if Argument_Count > 0 then
Ada.Text_IO.Open (File => File, Name => Argument (1), ...);
Ada.Text_IO.Set_Input (File => File);
end if;
loop
Get_Line (Item => Line, Last => Last);
-- Use Line
end loop;
if Argument_Count > 0 then
Ada.Text_IO.Close (File => File);
end if;
--
Jeff Carter
"Many times we're given rhymes that are quite unsingable."
Monty Python and the Holy Grail
57
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Newbie Question: using Text_IO.Open() to read from standard input
2005-10-13 5:26 ` Jeffrey R. Carter
@ 2005-10-13 6:53 ` tmoran
0 siblings, 0 replies; 11+ messages in thread
From: tmoran @ 2005-10-13 6:53 UTC (permalink / raw)
>> to use things from ARM 10.3 "Default Input, Output, and Error Files".
>...
>These procedures read from Current_Input rather than Standard_Input to allow a
>program to read from a file if one if provided and from Standard_Input if not:
Also, sometimes you have code that takes a file, eg
procedure Process_Data(F : in Ada.Text_IO.File_Type) is ...
and you want to call it with a named file or with Standard Input
Default_Data : Ada.Text_IO.File_Type;
begin
if ... then
Process_Data(Ada.Text_IO.Standard_Input);
else
Ada.Text_IO.Open(Default_Data, ...
Process_Data(Default_Data);
Ada.Text_IO.Close(Default_Data);
end if;
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: Newbie Question: using Text_IO.Open() to read from standard input
2005-10-13 1:48 ` pamela.lum
2005-10-13 1:56 ` Anh Vo
@ 2005-10-13 16:14 ` Martin Krischik
1 sibling, 0 replies; 11+ messages in thread
From: Martin Krischik @ 2005-10-13 16:14 UTC (permalink / raw)
pamela.lum@gmail.com wrote:
> Tried that (see the code below named test_cl.ada). It fails to detect
> arguments after the redirect ("<" ) character in the command :
Most commandline shells will strip the filename which follows "<" and ">"
from the commandline. Instead they will use the filename to open a File an
connect it to Standart_Input or Standart_Output of the newly created
process.
In fact: they will try to open the file *before* the program is started.
Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2005-10-13 16:14 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-13 0:49 Newbie Question: using Text_IO.Open() to read from standard input pamela.lum
2005-10-13 1:30 ` Anh Vo
2005-10-13 1:48 ` pamela.lum
2005-10-13 1:56 ` Anh Vo
2005-10-13 16:14 ` Martin Krischik
2005-10-13 1:58 ` tmoran
2005-10-13 2:02 ` pamela.lum
2005-10-13 2:19 ` Anh Vo
2005-10-13 5:26 ` Jeffrey R. Carter
2005-10-13 6:53 ` tmoran
2005-10-13 3:15 ` Steve
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox