Solved

How do you Print a Procedure?

Posted on 1998-04-09
7
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A look at what happened in the Verizon cloud breach.
Part Two of the two-part Q&A series with MalwareTech.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 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