Solved

How do you Print a Procedure?

Posted on 1998-04-09
7
184 Views
Last Modified: 2010-04-16
Hi,
I'm a beginning programmer who is having some difficulties.
I'm wondering how you can call a procedure to output the values of its' variables to a text file instead of to the screen. I know how to output information to a text file but I need to print variables from a procedure without writing out all of the statements from the procedure to the main program again. Please show me a small example if this is possible.  

Thanks,

Pyramid
0
Comment
Question by:Pyramid
7 Comments
 
LVL 2

Expert Comment

by:omsec
ID: 1217454
There are two types of variables, called LOCOAL VARIABLES used in a procedure or a function. The first ones are the parameter-variables you give as arguments to the procedure, and the second ones are internal variables of the procedure.

procedure WriteToFile(YourName : String);

Var
  YourCode : Byte;

You see, the variable YourName is an argument variable, the parameter of the procedure and YourCode is an internal, local variable.

Note, you can't access local variables from any others points in your programs. They're only avaiable in their procedures or functions, this is why they are called local variables. the opposit of a local variable is a global variable.

So ok, enough of "book stuff"

below you see a procedure, that writes some variables and constants into a file, and how to call that from a program :


Program Examples;

Uses
  Crt;

Var
  WriteTo : String;

Procedure WriteToFile(FileName : String);

Var
  FirstName  : String[20];
  FamilyName : String[20];
  Age        : Byte;
  FileHandle : Text;

Begin
  WriteLn;
  WriteLn('This text comes from the procedure..');
  WriteLn;
  WriteLn('Enter your Name : ');
  ReadLn(FirstName);
  WriteLn;
  WriteLn('Enter your Family Name : ');
  ReadLn(FamiliyName);
  WriteLn;
  WriteLn('Enter your Age : ');
  ReadLn(Age);
  Assign(FileHandle, FileName);
  Rewrite(FileHandle);
  WriteLn(FileHandle);
  WriteLn(FileHandle, 'This is an auto-generated file.');
  WriteLn(FileHandle);
  WriteLn(FileHandle, 'His/Her Name is ',FirstName);
  WriteLn(FileHandle, 'His/Her Family Name is ',FamilyName);
  WriteLn(FileHandle, 'His/Her Age is ',Age);
  WriteLn(FileHandle);
  Close(FileHandle);
  WriteLn;
End;

Begin
  WriteLn;
  WriteLn('Enter the file you wanna write in...');
  ReadLn(WriteTo);
  WriteToFile(WriteTo);
End.

That's all. If you need some others, like reading writing binary files or you need additional info, u may leave a comment. I didnt include any IO (input - output checks) to keep the example code small.

Hope that helps, greets, Omsec
0
 

Author Comment

by:Pyramid
ID: 1217455
Thanks for the reply!

I just need a little extra. In your example, if I wanted say, just the first & last name written to the screen and all of it written to a text file, how would I do this using the same procedure? Again, if this is possible.

Basically, if I was using this procedure to calculate the results of 3 variables x,y,z and I had to make 2 calls from the main program to this procedure( 1st call to print x and y to screen and 2nd call to print x,y,z to a text)how could I do this without printing x,y,z to the screen.

I need to find the most efficient way of using a procedure without duplicating its contents throughout the program.

Thanks in advance!

Pyramid
0
 

Expert Comment

by:nibbler
ID: 1217456
Have you ever heard of redirection ??
You must NOT use crt, (or set the variable "direct video" to false).

example, if you executable is test.exe you could enter this:

test > output.txt

In the file output.txt you will see the results you would otherwise see on the screen. The values in the writeln statements will be put into the file..

I hope this is the correct answer.. (I may have misunderstood your question !!)
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 2

Expert Comment

by:omsec
ID: 1217457
i dont think that is what he asked
0
 

Author Comment

by:Pyramid
ID: 1217458
Hi!

Thanks for the reply, Nibbler. To be honest with you, I really have no idea what you are talking about. I don't know that much about pascal. Omsec's reply is a lot closer to the answer I'm looking for. I do appreciate your help, though. If anybody needs any clarification on this problem please ask me.

Thanks,

Pyramid
0
 
LVL 3

Accepted Solution

by:
jlove1 earned 50 total points
ID: 1217459
Here's an answer I think you may enjoy. I don't know if it's EXACTLY what you're asking for, so I'll include 2 answers.

here's the first

type
mytype = object
 Variable : string;   {or whatever type}
 tofile : boolean;     {this chooses weather or not it will output to file}
 display : boolean;   {This chooses weather or not it will output to screen}
 end;
var
test1,test2,test3 : mytype ; {creates 3 of my objects}
procedure doit(var1 , var2 ,var3 : mytype) ;

var
f : text;
filename : string;
begin;
filename := 'testfile.txt';
assign(f,filename);
rewrite(f);
{do whatever you have to do here}

{...}

if var1.tofile then writeln(f,var1.variable);
if var1.display then writeln(var1.variable);

if var2.tofile then writeln(f,var2.variable);
if var2.display then writeln(var2.variable);

if var3.tofile then writeln(f,var3.variable);
if var3.display then writeln(var3.variable);

close(f);
end;

begin
test1.variable := 'this is some To File  ONLY DATA';
test1.tofile := true;
test1.display := false;

test2.variable := 'This is some Display ONLY DATA';
test2.tofile := false;
test2.display := true ;

test3.variable := 'This will be on Screen and in File';
test3.tofile := true;
test3.display := true;

doit(test1,test2,test3);

readln
end.


That's one way to do it using an OBJECT.
In the above example, objects (collections of variables) are passed to the function DOIT()
The properties of the object (variable,tofile, and display) are described
variable : String The data part of the object.
Tofile : Boolean value -- Set to TRUE will output to file FALSE will not.
Display : Boolean Value -- Set to True will output to screen, False will not.



the next example is a simple one, and I don't think this is what you're asking, but it could be.

procedure (somestring : string; someotherstring, whatever : string);
var
f : text;

begin
{Somestring will go to the SCREEN ONLY}
{Someotherstring will go to the file only}
{Whatever will go to BOTH}
assign(f, "testfile");
rewrite(f);
writeln(somestring);

writeln(f,someotherstring);

writeln(f,whatever);
writeln(whatever);
end;


there you go.

if you're asking for a "one line" way of printing something to the screen AND to a file try this.

procedure screenandfile( myvar : string);
var
f : text
begin;
assign(f,"testfile");
rewrite(f);
writeln(myvar);
writeln(f,myvar);
end;

Then in your program use screenandfile() instead of writeln()..

I hope this lengthy explanation is all that you ever wanted to know and more about printing to the screen and to a file. Let me know if I can be of further assistance

0
 

Author Comment

by:Pyramid
ID: 1217460
Hi!

Your answer seemed to work ok with a few modifications but it did work.

Thanks,

Pyramid
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Disable ctrl+alt+del 7 680
Delphi Timer?   How do I schedule an event every hour on the hour? 2 849
default value in Delphi component 2 512
.dat problems 39 203
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Employees depend heavily on their PCs, and new threats like ransomware make it even more critical to protect their important data.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question