Board index » delphi » still having problems with text files to procedure

still having problems with text files to procedure

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...

program external_file_work(input,output);

uses
  crt,strings;
type
  files=array[0..200]of char;
var
  userfile:files;
  choice:integer;
  exfile:text;
  mainfile:string;

{**********************************************
 * This procedure provides a menu for the                *
 * user to use. They have seven choices, which         *
 * one is to exit.This program will be execute              *
 * ed as many times as the user wants.                    *
 **********************************************}
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;

{}
procedure display_file(mainfile1:string);

begin
  assign(exfile,'mainfile1');
  reset(exfile);
  while not eof do
    begin
      readln(exfile,userfile);
      writeln(userfile);
    end;
end;

begin{of main}
  clrscr;
  write('Enter the full file name that you wish to use ---> ');
  readln(mainfile);
  writeln(mainfile);
  choice:=0;
  while choice<>7 do
    begin
      menu(choice);
      display_file(mainfile);
    end;
readln;
end.

Here is my problem,
  I am working on a school program and I am stuck once again. The
problem is I am trying to figure out how to transfer a text file to a
procedure. In the main program the user enters in what text file that
they want to use. Then I have to do a procedure that will display what
is in the text file.  In the procedure I assign the file and reset it.
After that I have a while not end of file loop. In the loop I read in
the file and simply have a writeln followed by the variable I assigned
the text. When I run the program it always breaks at read in the file
name. Now I think I am doing it right by assigning the variable for the
text file as a string and sending it up to the function. I would gladly
appreciate it if anybody can give me suggestions to what I may be doing
wrong. Thank you all.
  sincerely
     M.Grothe

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----

 

Re:still having problems with text files to procedure


I think i've found a bug. You wrote something like this in the file
processing procedure:

Assign (blah,'mainfile1');

Forget those ''! User Assign (blah,mainfile1). If you write minfile1 in ''
signs, that means it's a given string, not a varible or paramter (means
"c:\mydir\mainfile1." the filename, not the one you gave the procedure in
calling line).

bye

Igor2

Re:still having problems with text files to procedure


Quote
Renee Grothe wrote in message <38FA9494.4ED36...@grothegraphx.com>...
>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...

>program external_file_work(input,output);

>uses
>  crt,strings;
>type
>  files=array[0..200]of char;
>var
>  userfile:files;
>  choice:integer;
>  exfile:text;
>  mainfile:string;

>{**********************************************
> * This procedure provides a menu for the                *
> * user to use. They have seven choices, which         *
> * one is to exit.This program will be execute              *
> * ed as many times as the user wants.                    *
> **********************************************}
>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;

>{}
>procedure display_file(mainfile1:string);

>begin
>  assign(exfile,'mainfile1');
>  reset(exfile);
>  while not eof do
>    begin
>      readln(exfile,userfile);
>      writeln(userfile);
>    end;
>end;

>begin{of main}
>  clrscr;
>  write('Enter the full file name that you wish to use ---> ');
>  readln(mainfile);
>  writeln(mainfile);
>  choice:=0;
>  while choice<>7 do
>    begin
>      menu(choice);
>      display_file(mainfile);
>    end;
>readln;
>end.

>Here is my problem,
>  I am working on a school program and I am stuck once again. The
>problem is I am trying to figure out how to transfer a text file to a
>procedure. In the main program the user enters in what text file that
>they want to use. Then I have to do a procedure that will display what
>is in the text file.  In the procedure I assign the file and reset it.
>After that I have a while not end of file loop. In the loop I read in
>the file and simply have a writeln followed by the variable I assigned
>the text. When I run the program it always breaks at read in the file
>name. Now I think I am doing it right by assigning the variable for the
>text file as a string and sending it up to the function. I would gladly
>appreciate it if anybody can give me suggestions to what I may be doing
>wrong. Thank you all.
>  sincerely
>     M.Grothe

Renee,

You basically have the right idea. Your problem is that you are
not very precise in the use of words and concepts. Statements
like "the user enters in what text file that they want to use" are
unclear at best and misleading at worst. Please let me rephrase
your problem:

1. The main program prompts the user for a file name
2. The main program passes that file name to a procedure
3. The procedure opens a text file of that name
4. The procedure reads every line from the text file
5. The procedure writes every line to the screen
6. The procedure closes the file

This, by the way, is called pseudo-code. It should be the
the first step of every program you write.

Now let me make some remarks about each of the steps:

1. The main program prompts the user for a file name
You chose "mainfile" as a string variable for the file name.
This is OK for Pascal but not a terribly good choice
because it can be confusing. How about "FileName"?

2. The main program passes that file name to a procedure
No problem there.

3. The procedure opens a text file of that name
You use the correct way to open the file but omit to check
if the open is successful. This is bad news! Try this instead:
assign (exfile, FileName1);
{$I-} reset(exfile) {$I+}
if ioresult = 0
then
begin
    . . . { Process the contents of the file }
end
else
begin
    . . . { Deal with the error condition }
end;

4. The procedure reads every line from the text file
You use the statement
readln(exfile,userfile);
"userfile" is a variable of type "array[1..200] of char
It should be a string variable, e.g.
var line: string;
. . .
readln(exefile, line);
There is no need to make "line" a global variable.
Declare it locally!

5. The procedure writes every line to the screen
No problem here, as long as you use a string variable.

6. The procedure closes the file
Oops - you never close the file!
close(exfile);

Good luck

KlausL

Re:still having problems with text files to procedure


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.

Re:still having problems with text files to procedure


Quote
Renee Grothe <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...

It usually helps a great deal.

Quote
> procedure display_file(mainfile1:string);
> begin
>   assign(exfile,'mainfile1');

And here is your problem. By enclosing mainfile1 in quotes, you are telling
the computer to open the file whose name is "mainfile1", whereas you want to
tell it to open the file whose name is stored in the variable mainfile1.
So change this to

assign(exfile, mainfile1);

and you should be able to read from the file.

--
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will
   sa...@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
______________________________|_______________________________________

Other Threads