Solved

How do you Print a Procedure?

Posted on 1998-04-09
7
182 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 2

Expert Comment

by:omsec
Comment Utility
i dont think that is what he asked
0
 

Author Comment

by:Pyramid
Comment Utility
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
Comment Utility
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
Comment Utility
Hi!

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

Thanks,

Pyramid
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now