INTOUCH® 4GL
Application Development Guide


Previous Contents Index

Example 5 - Printing to an Output File

This example shows how to create and print to an output file for report purposes. The _CHANNEL option lets INTOUCH assign the output channel number for the output file. The UNIQUE option lets INTOUCH create the output file name and the ASK statement provides the output file name. When reading the data from the output file, the EOF option in the "line input" statement checks for the end of the file.

In the following example, the variable "end_file?" could also be "end_file" without the "?". The "?" simply flags the variable as a condition variable meaning that it contains the results of a TRUE or FALSE test. The "?" makes the variable easy to identify.


        1   program application_6_8 
        10  clear 
            print at 1, 1:; 
        20  message 'Enter lst letter of last names to be selected ' & 
                + 'separated by commas' 
            line input 'Letters (A,B,C)': list$ 
            print 
        30  open structure cl: name 'tti_run:client' 
            out_channel = _channel 
            open #out_channel: unique, access output 
            ask #out_channel: name out_file$ 
        40  extract structure cl 
              include match(list$, cl(last)[1:1]) > 0 
              sort ascending by cl(last) 
            end extract 
        50  for each cl 
              print #out_channel: cl(id); tab(12); cl(last); ' '; cl(first); & 
                    ' '; cl(middle); tab(45); cl(city); ' '; cl(state) 
            next cl 
        60  close all 
        70  open #out_channel: name out_file$ 
            clear 
            print at 2, 10: 'The input selections were:  '; list$; 
            print at 3, 10: 'The output file name is:  '; out_file$ 
            print at 4, 10: 'The output file contains the following records' 
            print 
        80  do 
              line input #out_channel, eof end_file?: line$ 
              if  end_file?  then  exit do 
              print line$ 
            loop 
        90  end 



 
         The input selections were:  b,d,e,f,s,w 
         The output file name is:  USER:[TESTER]TMP_360001.TMP 
         The output file contains the following records 
 
80542      Brock Bud B                      Duluth MN 
80561      Derringer Dale D                 Los Angeles CA 
56009      Donaldson George 
80522      Errant Earl E                    Monterey CA 
80573      Farmer Fred F                    Miami FL 
20000      Smith Sam S                      Seattle WA 
32001      Waters Wayne W                   San Diego CA 
 

Example 6 - Moving Around Within a Structure

You might need to return to a previously read record. To do this, you can use the ASK STRUCTURE and the SET STRUCTURE statements to move around in the file.

This example uses the ASK/SET STRUCTURE: POINTER statements. The program extracts records and creates a collection of pointers. Some data is displayed and the user can get additional information on a selected item.


        1   program application_6_9 
        10  clear 
        20  open structure cl: name 'tti_run:client' 
        30  extract structure cl 
              sort by cl(last) 
            end extract 
        40  x = 0 
            print at 2,1:; 
            for each cl 
              x = x + 1 
              print x; tab(6); cl(last) 
              if  x = 12  then  exit for 
            next cl 
            tot$ = str$(x) 
        50  do 
              message 'Enter a sequence no. between 1 and ' + tot$ 
              input 'Sequence No.', at 17,1, & 
                    valid 'number; allow 1 to '+ tot$ : seq_no 
              if  _back  or  _exit  then 
                clear 
                exit do 
              end if 
              set structure cl: pointer seq_no 
              ask structure cl: pointer ptr 
              print 
              print 'This record is pointer number '; ptr 
              print cl(id); tab(12); cl(last); ', '; cl(first); & 
                    tab(40); cl(city); ' '; cl(state); tab(65); cl(phone) 
              clear area 17,1,17,80 
            loop 
        60  close structure cl 
        70  end 



  1   Abott 
  2   Brock 
  3   Cass 
  4   Derringer 
  5   Donaldson 
  6   Errant 
  7   Farmer 
  8   Johnson 
  9   Kent 
  10  Porter 
  11  Rodrigues 
  12  Smith 
 
 
 
 Sequence No.? 6 
 
 This record is pointer number  6 
 80522      Errant, Earl                Monterey CA              (408) 844-7676 
 
 
                     Enter a sequence no. between 1 and 12 

This is just one example using SET STRUCTURE. There are a number of options for the ASK STRUCTURE and SET STRUCTURE statements. All of the options are described in detail in the INTOUCH - A Guide to the Language manual.


Chapter 7
Working with Files

In INTOUCH, files contain unstructured data and are opened using a channel number. Files have records, but do NOT have fields. INTOUCH can read and store data in structures and in files.

Files provide you with places to store data. They can exist on various devices (i.e. disks, tapes, etc.) and are identified by a specific file specification (name). Data saved to a file is stored in data records. Once a file is open, you have access to all data records stored in the file.

INTOUCH keeps file manipulation simple. Files can be opened and closed as they are needed. Writing new records, as well as reading, updating and deleting existing records are simple tasks once the file is opened with the right access option. If the file does not already exist, it can easily be created with an INTOUCH statement.

7.1 Opening and Closing Files

Before you can do anything to a file in INTOUCH, the file must be opened. Once the file is open, you have access to its data records. The OPEN statement opens an existing file or creates a new file.

When you open a file, you need to assign it a channel. You can select from channels 1 through 95. Channel 0 is reserved for the terminal. If you do not know or care what channel is assigned to a file, you can use the INTOUCH _CHANNEL built-in function. This function simply grabs the next available channel number and assigns it to the file.

When you open a file, you must specify how the file is going to be used, the access mode. The three access modes are:

If no access mode is specified, the access defaults to INPUT.

The OPEN statement can be used to create and open a new file. If you are creating a new file, the access must be either OUTPUT or OUTIN.

When you open a file, you need to provide the file name. If you are creating a temporary output file and do not care what the name is, you can specify UNIQUE and let INTOUCH create a unique file name for you.

Once you have finished working with the files, all the files should be closed. You can use the CLOSE statement to close each file or use CLOSE ALL to close all the files.

Example


        1   program application_7_1 
        10  clear 
            out_ch1 = _channel 
        20  open #out_ch1: name 'test_file_1.tmp', access output 
            ask #out_ch1: name file_1$ 
            print at 1, 1: 'File '; file_1$; ' has just been opened...' 
        30  out_ch2 = _channel 
            open #out_ch2: unique, access output 
            ask #out_ch2: name file_2$ 
            print 
            print 'File '; file_2$; ' has just been opened...' 
            print 'This unique file name was created by INTOUCH' 
        40  out_ch3 = _channel 
            open #out_ch3: name 'test_file_3', unique, access output 
            ask #out_ch3: name file_3$ 
            print 
            print 'File '; file_3$; ' has just been opened...' 
            print 'INTOUCH made the file name TEST_FILE_3 into a unique name' 
        50  close all 
        60  end 



File USER:[TESTER]TEST_FILE_1.TMP has just been opened... 
 
File USER:[TESTER]TMP_4E0001.TMP has just been opened... 
This unique file name was created by INTOUCH 
 
File USER:[TESTER]TEST_FILE_3_4E0002.TMP has just been opened... 
INTOUCH made the file name TEST_FILE_3 into a unique name 

7.2 Storing and Reading File Data

To store data in a file, you use the PRINT statement to write out the data. The file must first be opened with the access mode OUTPUT or OUTIN before data can be written to the file.

Writing data to a file is similar to printing data to the screen, except that a channel is specified. A typical PRINT statement defaults to channel 0, which is the terminal. Therefore, to send data to a file, just specify the channel assigned to the file in the PRINT statement.

Once a file is opened in INTOUCH, you have access to all the records until the file is closed. You can always read data from the file. After the file is opened, the data can be sequentially read in by element or by line using the INPUT and LINE INPUT statements, respectively. The OPEN statement makes the first record current (i.e. available).

When reading records from a file, you usually need to know when you have read the last record. INTOUCH allows you to check for the end-of-file condition by using the EOF option of the LINE INPUT statement.

The EOF option of the INPUT statement causes INTOUCH to return TRUE (i.e. 1) for a true condition or FALSE (i.e. 0) for a false condition, if the end of the file is or is not reached, respectively. The following example shows how the IF/THEN conditional construct tests for an end-of-file condition.

Example


        1   program application_7_2 
        10  clear 
            print at 1, 1:; 
            out_ch = _channel 
        20  open #out_ch: name 'temp', unique, access output 
            ask #out_ch: name filename$ 
            for i = 1 to 5 
              text$ = 'Record ' + str$(i) 
              print #out_ch: text$ 
              print 'Writing out:  '; text$ 
            next i 
        30  close #out_ch 
        40  open #out_ch: name filename$ 
            print 
            do 
              line input #out_ch, eof endfile?: line$ 
              if  endfile?  then 
                print 'End of file - the last record has already been read' 
                exit do 
              end if 
              print 'Reading:  '; line$ 
            loop 
        50  end 



 
  Writing out:  Record 1 
  Writing out:  Record 2 
  Writing out:  Record 3 
  Writing out:  Record 4 
  Writing out:  Record 5 
 
  Reading:  Record 1 
  Reading:  Record 2 
  Reading:  Record 3 
  Reading:  Record 4 
  Reading:  Record 5 
  End of file - the last record has already been read 
 

Unformatted output files

There are times when an output file is to become the input file for some device (i.e. PC, plotter, etc.). When an output file is created for export, the UNFORMATTED option in the open statement can be used to suppress the control characters (i.e. carriage return, line feed) from the output data.

Example


        1   program application_7_3 
        10  clear 
            print at 1, 1:; 
            out_ch = _channel 
        20  open #out_ch: name 'file', unique, access output, unformatted 
            ask #out_ch: name filename$ 
            for i = 1 to 5 
              text$ = 'Item ' + str$(i) 
              print #out_ch: text$ 
              print 'Writing out:  '; text$ 
            next i 
        30  print #out_ch: 'This is the end of the file' 
            print 
        40  close all 
        50  command$ = 'type ' + filename$ 
            pass command$ 
            print 
        60  end 



 
  Writing out:  Item 1 
  Writing out:  Item 2 
  Writing out:  Item 3 
  Writing out:  Item 4 
  Writing out:  Item 5 
 
  Item 1Item 2Item 3Item 4Item 5This is the end of the file 
 

7.3 Positioning File Records

There are times when you are reading through a file and want to return to a previously read record. Instead of closing the file, reopening it (going back to the first record), and reading each successive record up to the one you want, you can accomplish the same task with two simple INTOUCH statements. Using the CURRENT option, with the ASK and SET statements enables you to switch between records.

Example


        1   program application_7_4 
        10  clear 
            print at 1, 1:; 
            out_ch = _channel 
            filename$ = 'temp.tmp' 
        20  open #out_ch: name filename$ , access output 
            for i = 1 to 10 
              text$ = 'Record ' + str$(i) 
              print #out_ch: text$ 
              print 'Writing out:  '; text$ 
            next i 
            close #out_ch 
            print 
        30  open #out_ch: name filename$ 
            input #out_ch: line1$ 
            print 'The 1st record is :  '; line1$ 
            for j = 1 to 3 
              input #out_ch: line2$ 
            next j 
            print 'The 4th record is :  '; line2$ 
        40  ask #out_ch: current cur_line$ 
            for k = 1 to 4 
              input #out_ch: line3$ 
            next k 
            print 'The 8th record is :  '; line3$ 
        50  set #out_ch: current cur_line$ 
            print 'Back to 4th record:  '; line2$ 
        60  close all 
        70  end 



 
  Writing out:  Record 1 
  Writing out:  Record 2 
  Writing out:  Record 3 
  Writing out:  Record 4 
  Writing out:  Record 5 
  Writing out:  Record 6 
  Writing out:  Record 7 
  Writing out:  Record 8 
  Writing out:  Record 9 
  Writing out:  Record 10 
 
  The 1st record is :  Record 1 
  The 4th record is :  Record 4 
  The 8th record is :  Record 8 
  Back to 4th record:  Record 4 
 


Chapter 8
Working with Data

When data is printed, displayed or stored in record fields, the data is often not in the required format. For example, when dollar amounts are printed on a report, you want to print the amount as $9,999,999.99 not as 9999999.99. In another case, you might have a string of data from which you need to extract a piece of information. INTOUCH provides many built-in functions to help you work with data.

8.1 Formatting Data

Often, when data is printed on reports or stored in record fields, the data needs to be changed to different formats. Formatting data can also involve data alignment and field justification.

There are a number of ways to format and align data using INTOUCH.

PRINT USING

The PRINT USING statement is most often used to print numbers but it can also be used to print characters. PRINT USING formats data according to the mask you provide. This statement allows you to specify the character type, field length, field alignment, etc. You can also insert special characters (i.e. "$" for dollar values, "." for numeric data, "+/-" for leading characters, etc.).

The INTOUCH - A Guide to the Language manual contains descriptions of all the format characters.

FORMAT$ Function

FORMAT$() works like PRINT USING. The difference is that PRINT USING is a print statement and FORMAT$ is a function. FORMAT$ can be used in a print statement.

Example

The following example shows how to use different variations of the PRINT USING statement and the FORMAT$ function.


        1   program application_8_1 
        10  clear 
            phone_no$ = '5551212' 
            dollars$ = '999999999' 
        20  print at 1, 15: 'Different variations can produce the same results' 
            print 
            print 'Dollar amount:'; 
            print using '$###,###,###.##': dollars$ 
            dollar_mask$  = '$###,###,###.##' 
            print 'Dollar amount:'; 
            print using dollar_mask$: dollars$ 
            print 
        30  z$ = format$(phone_no$, '###~-####') 
            print 'Phone number :  '; z$ 
            phone_mask$ = '###~-####' 
            print 'Phone number :  '; format$(phone_no$, phone_mask$) 
        40  end 



 
              Different variations can produce the same results 
 
Dollar amount:  $9,999,999.99 
Dollar amount:  $9,999,999.99 
 
Phone number :  555-1212 
Phone number :  555-1212 
 

LSET, RSET and CSET

The LSET, RSET and CSET statements allow you to format and align string data. You can also fill the variable or field with a designated value using the FILL option.

LPAD$, RPAD$ and CPAD$

The LPAD$(), RPAD$() and CPAD$() functions allow you to pad data on the left, right or both sides with a designated character. The function creates a new string containing the data and pad characters. If no pad character is specified, the default pad character will be a space.

Example

This example shows some formatting techniques.


        1   program application_8_2 
        10  clear 
            print at 1, 1:; 
        20  name_mask$ = '>@@@@@@@@@@' 
            ssn_mask$ = '###~-##~-####' 
            h_date$ = space$(15) 
            h_page$ = space$(15) 
            h_title$ = space$(40) 
            page_no = 1 
        30  input prompt 'Enter your name  : ', at 3,5: name$ 
            input prompt 'Soc. Security No.: ', at 4,5: ssn$ 
            input prompt 'Enter a number   : ', at 5,5: r_number 
            delay 
        40  lset h_date$ = date$[5:6] + '/' + date$[7:8] + '/' + date$[3:4] 
            rset h_page$ = 'Page ' + str$(page_no) 
            cset h_title$ = 'Examples of FORMATTING Data' 
            heading$ = h_date$ + h_title$ + h_page$ 
        50  clear 
            print bold, at 1, 1: heading$ 
            print at 3,1: 'Date was LSET  :  '; '('; h_date$; ')' 
            print 'Page was RSET  :  '; '('; h_page$; ')' 
            print 'Title was CSET :  '; '('; h_title$; ')' 
            print '     The 3 parts made up the heading' 
            print 
        60  print bold: cpad$('Examples of PRINT USING', 70, '-') 
            print 
            print 'Right justified  :', 
            print using name_mask$: name$ 
            print 'Screen formatted :', 
            print using ssn_mask$: ssn$ 
            print 'Preceeding zeros :', 
            print using '%%%%%%%%%%%': r_number 
            print 'Leading "+"      :', 
            print using '+##########': r_number 
            print 'Dollar format    :', 
            print using '$###,###,###.##': r_number 
            print 
        70  print bold: cpad$('Examples of Padding', 70, '-') 
            print 
            print 'RPAD$ - pads on the right  :  '; rpad$(name$, 30, '*') 
            print 'LPAD$ - pads on the left   :  '; lpad$(name$, 30, '*') 
            print 'CPAD$ - pads on both sides :  '; cpad$(name$, 30, '*') 
            delay 
        80  end 



    Enter your name  : Molly 
    Soc. Security No.: 123456789 
    Enter a number   : 730528 
 
 
 
 
                        Press the RETURN key to continue 



03/18/96              Examples of FORMATTING Data               Page 1 
 
Date was LSET  :  (03/18/96       ) 
Page was RSET  :  (         Page 1) 
Title was CSET :  (       Examples of FORMATTING Data      ) 
     The 3 parts made up the heading 
 
------------------------Examples of PRINT USING----------------------- 
 
Right justified  :        Molly 
Screen formatted :  123-45-6789 
Preceeding zeros :  00000730528 
Leading "+"      :      +730528 
Dollar format    :      $730,528.00 
 
--------------------------Examples of Padding------------------------- 
 
RPAD$ - pads on the right  :  Molly************************* 
LPAD$ - pads on the left   :  *************************Molly 
CPAD$ - pads on both sides :  *************Molly************ 
 
 
                        Press the RETURN key to continue 


Previous Next Contents Index