On Sun, 16 Apr 2000 21:35:32 -0700, Renee Grothe
Quote
<re...@grothegraphx.com> wrote:
Hello. I am still having some difficulty with my program. I thought if
i show the code maybe some one will be able to understand it better...
-------------- snip --------------------
procedure menu(var choice1:integer);
begin
writeln;
writeln('Menu: 1. Display the contents of the file');
writeln('2. Number the lines of the file':38);
writeln('3. Count the characters in the file':41);
writeln('4. Count the words in the file':37);
writeln('5. Replace each occurrence of one character with another
character':73);
writeln('6. Enter a new file name':31);
writeln('7. Exit the program':26);
writeln;
write('Pick what you would like to have done ---> ');
readln(choice);
end;
......................... snip .......................
sincerely
M.Grothe
Maybe this will give you some ideas but I doubt that your instructor
is expecting BLOCKREAD to be used. It is interesting that all your
file functions can be achieved by this method. You define an array of
chars (or bytes if you like) that is longer than the expected file
length, for example 60,000. Your file is declared as untyped and the
whole thing is dumped into your char array with a BLOCKREAD. The chars
include the end-of-line CR Chr(13) and LF Chr(10).
1. To show the file you simply write every char to the screen. You
could pause to read the screen with: If LineCt MOD 20 = 0 then Readln;
2. To count the number of lines (including blank lines) you simply
count the CRs OR LFs, not both.
3. If you just want the alpha-numeric chars you exclude all spaces,
CR, LF, punctuation, etc. just define the set you want such as:
allowed = ['A'..'Z', 'a'..'z', '0'..'9'] and compare each char in
the array, such as: If A[ct] in allowed then Inc(CharCt);
4. You count the words by parsing the array using the "allowed" above
to start the word and NOT A[ct] IN ALLOWED to end the word.
5. You can scan the array stopping on the char to be replaced and put
your replacement at that array address, A[CharCt] := 'P', for example.
You should use the char array in this case as a global variable rather
than passing it as a parameter so you will not run out of stack space.
This is the way it starts:
Program DumpFile;
CONST max = 60000;
VAR
f:FILE;
A:Array[1..max] of Char;
actual, CharCt, LineCt:Word;
Begin
Assign(f, 'MYJUNK.TXT');
Reset(f, 1); {for 1 char per array element}
BLOCKREAD(f, A, max, actual);
Close(f);
Now every char (whether alphabetic or not) up to 60000 is in array A.
The actual length of the array is "actual". The following displays the
text and counts the lines:
LineCt := 0;
For CharCt := 1 to actual Do
Begin
Write(A[CharCt];
If A[CharCt] = Chr(10) then
Begin
Inc(LineCt);
If LineCt Mod 20 = 0 Then Readln;
End;
End;
Writeln; Writeln('Line count = ', LineCt);
and so on.