Reading info from text file... 
Author Message
 Reading info from text file...

I have a program that I've been working on now and then that finds the '%'
character and then reads in the lines until another '%' character is reached.  In
the past, I had my program just read in each line and
immediately display it to the screen.  Now, I'd like to give the user the choice
of either displaying only to the screen or write the lines to a specific text
file.  I could just setup a few boolean variables and test the condition before
each writeln but I'd rather load up an array or something and have one read
procedure with two distinct output procedures called based on one or two boolean
variables.

The infile is setup like this:

%

Some stuff
of
variable
lengths (max 80) and lines (max 20).

%

I thought about having an array but I've gotten myself confused with the
indexing.  Does an array of [1..20, 1..80] make sense in this case?  Would I need
to employ the move command to load another variable in order to write the
information to the screen/file?  I think I've thoroughly confused myself with
this one.  :)  It's got to be easier than I'm making it out to be.

If someone would please point me in the right direction, I would greatly
appreciate it.

--



Fri, 19 Jul 2002 03:00:00 GMT  
 Reading info from text file...

Quote:

>The infile is setup like this:

>%

>Some stuff
>of
>variable
>lengths (max 80) and lines (max 20).

>%

>I thought about having an array but I've gotten myself confused with the
>indexing.  Does an array of [1..20, 1..80] make sense in this case?  Would I need
>to employ the move command to load another variable in order to write the
>information to the screen/file?  I think I've thoroughly confused myself with
>this one.  :)  It's got to be easier than I'm making it out to be.

Type str80 : string[80];

Var  LineArray : array[1..20] of str80;

Make an array with 20 elements (the number of lines) each containing a
string with length 80 (the line).
If the number of lines varies it would be nicer to make a linked list,
but the array should work fine.

Bas



Sat, 20 Jul 2002 03:00:00 GMT  
 Reading info from text file...


Quote:

>>The infile is setup like this:

>>%

>>Some stuff
>>of
>>variable
>>lengths (max 80) and lines (max 20).

>>%

>>I thought about having an array but I've gotten myself confused with the
>>indexing.  Does an array of [1..20, 1..80] make sense in this case?  Would I need
>>to employ the move command to load another variable in order to write the
>>information to the screen/file?  I think I've thoroughly confused myself with
>>this one.  :)  It's got to be easier than I'm making it out to be.

>Type str80 : string[80];
>Var  LineArray : array[1..20] of str80;
>Make an array with 20 elements (the number of lines) each containing a string
>with length 80 (the line).
>If the number of lines varies it would be nicer to make a linked list, but the
>array should work fine.

Is that the same as LineArray : array[1..20, 80] of string?

--



Sat, 20 Jul 2002 03:00:00 GMT  
 Reading info from text file...

Quote:

>>Type str80 : string[80];

>>Var  LineArray : array[1..20] of str80;

>>Make an array with 20 elements (the number of lines) each containing a string
>>with length 80 (the line).
>>If the number of lines varies it would be nicer to make a linked list, but the
>>array should work fine.

>Is that the same as LineArray : array[1..20, 80] of string?

No the declaration you make is illegal. It should state [1..20,1..80]
and even then it is different.

Maybe a visualization will clear things up.

my array looks like this:

1(contains line one)
2(contains line 2)
.
.
19(contains line 19)
20(contains line 20)

You have one column of 20 elements.

[1..20,1..80] looks like this :

1  2  .  .   .     ...   79 80
2
.
.
19
20
this array has 80 columns and 20 rows of elements, that makes 1600
elements.
I hope this helps

Bas



Sun, 21 Jul 2002 03:00:00 GMT  
 Reading info from text file...

Quote:

>>No the declaration you make is illegal. It should state [1..20,1..80] and even
>>then it is different.

>Sorry.  That was a typo.

>>Maybe a visualization will clear things up.

>>my array looks like this:

>>1(contains line one)
>>2(contains line 2)
>>..
>>..
>>19(contains line 19)
>>20(contains line 20)

>>You have one column of 20 elements.

>>[1..20,1..80] looks like this :

>>1  2  .  .   .     ...   79 80
>>2
>>..
>>..
>>19
>>20
>>this array has 80 columns and 20 rows of elements, that makes 1600
>>elements.
>>I hope this helps

>If you pre-define a 20 element array of type array[80], why wouldn't yours have
>1600 elements also?

>Please forgive my denseness :)

the first representation is array[1..20] of string[80] which is in
fact the same as the second one since Pascal handles strings as
arrays, but it is easier to use the string type then an array of
characters.
So for your needs is easier to use array[1..20] of string[80] then an
array[1..20,0..80] of char.

Bas



Mon, 22 Jul 2002 03:00:00 GMT  
 Reading info from text file...


Quote:

>I have a program that I've been working on now and then that finds the '%'
>character and then reads in the lines until another '%' character is
reached.  In
>the past, I had my program just read in each line and
>immediately display it to the screen.  Now, I'd like to give the user the
choice
>of either displaying only to the screen or write the lines to a specific
text
>file.  I could just setup a few boolean variables and test the condition
before
>each writeln but I'd rather load up an array or something and have one read
>procedure with two distinct output procedures called based on one or two
boolean
>variables.

.............. snip ......................

Although you have had a lot of responses from your request, here is the
framework I have used on several occasions for similar actions. By using
BlockRead you can dump 60,000+ chars into an array very rapidly. That char
array has all the carrage returns ( CR = Chr(13) ) and line feeds ( LF =
Chr(10) ) as well as readable chars. Modifcations can be made but for my
purposes (addresses, bibliographies and personal help files) 60K is more
than enough. If you display all that at once that would be at least 30
screens.

These end of line chars can be used with another char to recognize the
beginning and ending of the section of text you select:

"[" + CR + LF   (this represents a sequence of array chars)
This is the first chars of the text item.....
....and so on to last line of the item which ends with CR LF
"]" (on the line following)

The square brackets are my choice because they are easily remembered and
normally, [ is not at the end of a line nor ] at the beginning. In addition
you can just use ][ between text items. You can also put remarks and
references between ends and starts without interfering with the program.

To scan the array for a match I use tag:String[3] as a kind of char stack by
setting tag := '' and, within the loop reading the array chars, use:
           tag := A[ct] + tag;
This will keep putting the array chars at the tag front and pushing the
third off the string. This makes the tag appear in the reverse order of
reading. In your CONST delarations you can do something like:
CONST    LF = Chr(10);  CR = Chr(13);
         go = LF + CR + '[';    stop = ']' + LF + CR;
and compare tag to go and stop.

Make two index arrays or one 2D array to store the array positions for start
and finish of each item of text such as indx[1..100, 1..2] of Word;

By making the first line of the text item descriptive, you can make a
numerical menu which displays just the first line by stopping after the
first LF. You can select by number (which is also the array primary index)
to view the item as well as to copy the text to another file with
BlockWrite.

This is enough for the general interest. I'll email some on hand source code
later.




Tue, 23 Jul 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Read an external TEXT files and store info in 2 Separate ARRAYS

2. Assess text file info using ttAscii and Schema file

3. Text File Read / Dbase file Write

4. reading a Unix Generated text file

5. How can read a text file?

6. Reading a text file

7. read text file

8. How to be reading a text file

9. How can I read text files into variables?

10. reading from text files

11. reading from text files

12. Reading text file from pascal

 

 
Powered by phpBB® Forum Software