* how to read a file into a array???
@ 2002-05-04 18:35 devine
2002-05-04 19:05 ` Jeffrey Carter
0 siblings, 1 reply; 9+ messages in thread
From: devine @ 2002-05-04 18:35 UTC (permalink / raw)
I'm having some problems reading a file into a array. Say the file
has the line
This is a test, how can i read each word of the file into a array? an
example would be helpful.
thanks.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how to read a file into a array???
2002-05-04 18:35 how to read a file into a array??? devine
@ 2002-05-04 19:05 ` Jeffrey Carter
2002-05-04 22:24 ` Mark Biggar
0 siblings, 1 reply; 9+ messages in thread
From: Jeffrey Carter @ 2002-05-04 19:05 UTC (permalink / raw)
devine wrote:
>
> I'm having some problems reading a file into a array. Say the file
> has the line
> This is a test, how can i read each word of the file into a array? an
> example would be helpful.
> thanks.
An array object in Ada is always a fixed size, so you have to have some
means of deciding how big to make your array. Since words in a text file
are different sizes, you're going to have to have array components that
can handle different size components.
Then there's the definition of a "word" in a text file. Many definitions
are possible. For example, is punctuation part of a word? One definition
of "word" is used by PragmARC.Word_Input (available from
http://home.earthlink.net/~jrcarter010/pragmarc.htm
and the mirror at www.adapower.com).
Until you have dealt with these, it's difficult to be more helpful.
--
Jeff Carter
"Nobody expects the Spanish Inquisition!"
Monty Python's Flying Circus
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how to read a file into a array???
2002-05-04 19:05 ` Jeffrey Carter
@ 2002-05-04 22:24 ` Mark Biggar
2002-05-05 2:18 ` devine
2002-05-05 6:20 ` how to read a file into a array??? Simon Wright
0 siblings, 2 replies; 9+ messages in thread
From: Mark Biggar @ 2002-05-04 22:24 UTC (permalink / raw)
Jeffrey Carter wrote:
>
> devine wrote:
> >
> > I'm having some problems reading a file into a array. Say the file
> > has the line
> > This is a test, how can i read each word of the file into a array? an
> > example would be helpful.
> > thanks.
>
> An array object in Ada is always a fixed size, so you have to have some
> means of deciding how big to make your array. Since words in a text file
> are different sizes, you're going to have to have array components that
> can handle different size components.
Don't bother. Slurp the whole file into one big string and then
parse and create an array of records containing indices to the
first and last character of each word. Write an access function
that takes an index and returns the corresponding slice.
--
Mark Biggar
mark.a.biggar@attbi.com
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how to read a file into a array???
2002-05-04 22:24 ` Mark Biggar
@ 2002-05-05 2:18 ` devine
2002-05-05 3:52 ` Dale Stanbrough
2002-05-17 8:50 ` GNU Readline ? Sylvain NAHAS
2002-05-05 6:20 ` how to read a file into a array??? Simon Wright
1 sibling, 2 replies; 9+ messages in thread
From: devine @ 2002-05-05 2:18 UTC (permalink / raw)
This is what I have so far:
WITH Ada.Text_IO;
WITH Ada.Integer_Text_IO;
PROCEDURE Test IS
FileName : String(1..80);
MaxName : CONSTANT Positive := 80;
SUBTYPE NameRange IS Positive RANGE 1..MaxName;
Length : NameRange;
InFile : Ada.Text_IO.File_Type;
y: integer;
Max : CONSTANT Integer := 999999;
SUBTYPE Size IS Integer RANGE 0..Max;
TYPE TestArray IS ARRAY (Size) OF Character;
X : TestArray;
BEGIN
Ada.Text_IO.Put (Item => "Enter the name of the file > ");
Ada.Text_IO.Get_Line(Item => FileName, Last => Length);
Ada.Text_IO.New_Line;
Ada.Text_IO.Open(File => InFile, Mode => Ada.Text_IO.In_File, Name
=> FileName(1..Length));
Y:=0;
LOOP
EXIT WHEN Ada.Text_IO.End_Of_File(InFile);
Y:=Y+1;
Ada.Text_IO.Get(File => InFile, Item => X(Y));
Ada.Text_IO.New_Line;
END LOOP;
Ada.Integer_Text_IO.Put(y);
END Test;
This is what I was playing around with. How can I change it so that
it reads the file and each word is read instead of each character? i
put character at the top because i wasn't sure what to put their. I
will need something that checks for the whitespace inbetween the words
right?
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how to read a file into a array???
2002-05-05 2:18 ` devine
@ 2002-05-05 3:52 ` Dale Stanbrough
2002-05-17 8:50 ` GNU Readline ? Sylvain NAHAS
1 sibling, 0 replies; 9+ messages in thread
From: Dale Stanbrough @ 2002-05-05 3:52 UTC (permalink / raw)
devine@linuxmail.org (devine) wrote:
> This is what I have so far:
> WITH Ada.Text_IO;
> WITH Ada.Integer_Text_IO;
> PROCEDURE Test IS
>
> FileName : String(1..80);
> MaxName : CONSTANT Positive := 80;
> SUBTYPE NameRange IS Positive RANGE 1..MaxName;
> Length : NameRange;
> InFile : Ada.Text_IO.File_Type;
> y: integer;
> Max : CONSTANT Integer := 999999;
> SUBTYPE Size IS Integer RANGE 0..Max;
> TYPE TestArray IS ARRAY (Size) OF Character;
> X : TestArray;
>
> BEGIN
>
> Ada.Text_IO.Put (Item => "Enter the name of the file > ");
> Ada.Text_IO.Get_Line(Item => FileName, Last => Length);
> Ada.Text_IO.New_Line;
> Ada.Text_IO.Open(File => InFile, Mode => Ada.Text_IO.In_File, Name
> => FileName(1..Length));
>
> Y:=0;
> LOOP
> EXIT WHEN Ada.Text_IO.End_Of_File(InFile);
> Y:=Y+1;
> Ada.Text_IO.Get(File => InFile, Item => X(Y));
> Ada.Text_IO.New_Line;
> END LOOP;
>
> Ada.Integer_Text_IO.Put(y);
Using Mike Feldman's text book?
I wonder who set this assignment...
There is no magic that Ada will provide you to solve
this problem. You have to figure out how to store a word,
or how to know where a word starts and ends.
You should be able to draw a picture of how your data will
be structured, and be able to explain it to someone
coherently before you start coding.
Then you have to transfer that idea into code.
Dale
^ permalink raw reply [flat|nested] 9+ messages in thread
* GNU Readline ?
2002-05-05 2:18 ` devine
2002-05-05 3:52 ` Dale Stanbrough
@ 2002-05-17 8:50 ` Sylvain NAHAS
2002-05-17 10:25 ` Adrian Knoth
1 sibling, 1 reply; 9+ messages in thread
From: Sylvain NAHAS @ 2002-05-17 8:50 UTC (permalink / raw)
Please,
Is there an Ada port/bind of the GNU Readline library around ?
Thanks.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: how to read a file into a array???
2002-05-04 22:24 ` Mark Biggar
2002-05-05 2:18 ` devine
@ 2002-05-05 6:20 ` Simon Wright
1 sibling, 0 replies; 9+ messages in thread
From: Simon Wright @ 2002-05-05 6:20 UTC (permalink / raw)
Mark Biggar <mark.a.biggar@attbi.com> writes:
> Don't bother. Slurp the whole file into one big string and then
> parse and create an array of records containing indices to the first
> and last character of each word. Write an access function that
> takes an index and returns the corresponding slice.
This isn't a general solution, though; how long a string do you need?
I suppose you could use file io operations, if your compiler provides
them, to work out the length first .. it seems unwise to get into the
habit of assuming a maximum size to whatever your input may be.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2002-05-17 19:21 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-04 18:35 how to read a file into a array??? devine
2002-05-04 19:05 ` Jeffrey Carter
2002-05-04 22:24 ` Mark Biggar
2002-05-05 2:18 ` devine
2002-05-05 3:52 ` Dale Stanbrough
2002-05-17 8:50 ` GNU Readline ? Sylvain NAHAS
2002-05-17 10:25 ` Adrian Knoth
2002-05-17 19:21 ` Thomas Dickey
2002-05-05 6:20 ` how to read a file into a array??? Simon Wright
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox