garnett@a.cs.okstate.edu (John Garnett) (03/14/88)
This is a Turbo Pascal question: I want to be able to use one writeln statement to write to the printer, screen, or a file. What I am thinking of is something like this: writeln(fd,'Hello') where by changing the value of fd I could cause the writeln to act like writeln(lst,'Hello'); or writeln(outfile,'Hello'); where outfile is defined : var outfile:text; or writeln('Hello') { to the screen }. The reason I would like to be able to do this is because I am tired of writing three separate sections which each do essentially the same thing. I don't know what type fd would have to be or if this is even possible in Turbo Pascal. Any ideas or solutions would be appreciated. Thanks. +-------------------------------------------------------------------------+ | john garnett Internet: garnett@a.cs.okstate.edu | | computing and information sciences UUCP: {cbosgd, ihnp4, rutgers}! | | oklahoma state university okstate!garnett | +-------------------------------------------------------------------------+ | "Since the mathematicians have attacked the relativity theory, | | I myself no longer understand it." - Albert Einstein | +-------------------------------------------------------------------------+
russ@hpldola.HP.COM (Russell Johnston) (03/17/88)
> This is a Turbo Pascal question: > I want to be able to use one writeln statement to write to the printer, > screen, or a file. What I am thinking of is something like this: > writeln(fd,'Hello') The following works in Turbo 3.0: var fb:text; DISK,PRINTER:boolean; begin . . . if DISK then assign(fb,'myfile.txt') else if PRINTER then assign(fb,'LST:') else assign(fb,'CON:'); writeln(fb,'Hello'); If anyone has a solution for Turbo 4.0, I would like to see it.
taylorj@byuvax.bitnet (03/17/88)
Here's what you need for a writeln that will write to anything. Define fd as a text file: var fd :text; Then to write to the screen: assigncrt(fd); This assigns the output file to Turbo Pascal 4.0's fast (very fast!) CRT driver. (If you're using Turbo 3.0, use assign(fd, 'con') instead.) To write to a file, do a standard assign: assign(fd, 'filename'); To write to the printer, assign to a device name: assign(fd, 'prn'); You can use any DOS device name such as 'aux', 'com1', 'lpt1', etc. Last, (and importantly) open your output file for writing to: rewrite(fd); Then you can use writeln(fd, 'Whatever'); I use this in most of my programs, so I know it works. Have fun! Jim Taylor Microcomputer Support for Curriculum, Brigham Young University taylorj@byuvax.bitnet
garnett@a.cs.okstate.edu (John Garnett) (03/18/88)
From article <3297@okstate.UUCP>, by garnett@a.cs.okstate.edu (John Garnett): > > I want to be able to use one writeln statement to write to the printer, > screen, or a file. What I am thinking of is something like this: > > writeln(fd,'Hello') > > where by changing the value of fd I could cause the writeln to act > like writeln(lst,'Hello'); or writeln(outfile,'Hello'); where outfile > is defined : var outfile:text; or writeln('Hello') { to the screen }. > In case anyone else wants to know how to do this also, I am posting a solution that is typical of the ones I received: Program test; var outfile: Text; {The file or device to write to} procedure output_line(var f: Text; Message: integer); begin writeln(f,"This is message #",Message,"."); end; begin assign(outfile,'test.dat'); rewrite(outfile); output_line(lst,1); output_line(con,2); output_line(outfile,3); close(outfile); end. Thanks to: John Cusack, Richard Ward, Russell Taylor, Paul Neubauer, and Jim Slack for providing solutions... +-------------------------------------------------------------------------+ | john garnett Internet: garnett@a.cs.okstate.edu | | computing and information sciences UUCP: {cbosgd, ihnp4, rutgers}! | | oklahoma state university okstate!garnett | +-------------------------------------------------------------------------+ | "Since the mathematicians have attacked the relativity theory, | | I myself no longer understand it." - Albert Einstein | +-------------------------------------------------------------------------+
jws@hpcljws.HP.COM (John Stafford) (03/18/88)
I don't know turbo, but if it support passing files as parameters (standard Pascal does), just put the writeln in a procedure that takes the file to use as a parameter. PROGRAM something (input, output); VAR f1: TEXT; f2: TEXT; PROCEDURE writeit (VAR f: TEXT); BEGIN writeln (f, 'Hello World'); END; BEGIN (* Do whatever one does to associate Pascal file variables with external files here. This is no doubt wrong for Turbo, it is how HP Pascal would do it... i.e. something like rewrite (f1, 'SCREEN:'); rewrite (f2, 'PRINTER:'); *) writeit (f1); writeit (f2); END.