chap06 - QBASIC Chapter 6 QBASIC Chapter 6 Sequential Files...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: QBASIC Chapter 6 QBASIC Chapter 6 Sequential Files, Paging Reports, Sequential and Control-Break Processing and Created by Richard D. Taylor Illustrated by Saundra J. Taylor Introduction Introduction ­ The use of data files ­ Writing programs that generate reports of Writing more than one page more ­ Control breaks - technique used to generate Control subtotals within a report subtotals Data Files Data Files ­ File-handling statements - software to deal directly with the organization and processing of large amounts of data processing ­ File - group of related records QBASIC File handling Statements QBASIC File handling Statements ­ Create data files ­ Define the data files to be used by a program ­ Open a file ­ Read data from a file ­ Write data to a file ­ Test for the end-of-file ­ Close a file File Organization File Organization ­ A method of arranging records on an method auxiliary storage device auxiliary ­Sequential files - the records can be processed Sequential only in the order in which they are placed in the file file ­Random files - discussed in Chapter 9 Sequential File Processing Sequential File Processing ­ Opening Sequential Files OPEN ­Requests the PC to allocate a buffer ­ Buffer - part of main storage through which data is passed Buffer between the program and auxiliary storage between ­Identifies by name the file to be processed ­Indicates whether the file is to be read from or Indicates written to written ­Assigns the file a filenumber ­Sets the “pointer” to the beginning of the file or to Sets the end of the file the OPEN Statement for Sequential OPEN Statement for Sequential Files ­ General Form: ­OPEN filespec FOR mode AS #filenumber filespec is the name of the file ­ mode ­ ­APPEND - adds records to the end of the file ­INPUT - puts records at the beginning of the new file ­OUTPUT - adds records to the beginning of the file ­ Purpose - allows a program to read from or Purpose write records to a sequential file write OPEN Statement for Sequential OPEN Statement for Sequential Files ­ Examples ­OPEN “B:EMPLOYEE.DAT” FOR INPUT AS #1 ­OPEN “payroll.lis” FOR OUTPUT AS #2 ­OPEN “ACCOUNTS.DAT” FOR APPEND AS #3 ­OPEN filename$ FOR INPUT AS #3 Summarization of Rules of the Summarization of Rules of the OPEN Statement ­ A sequential file must be opened before it sequential can be read from or written to can ­ A program can read only records from a program sequential file that has been opened for input input ­ A sequential file must already exist if it is sequential opened for input opened Summarization of Rules of the Summarization of Rules of the OPEN Statement ­ A program can write only records to a program sequential file that has been opened for output or to append or ­ A filenumber can be assigned to only one filenumber sequential file at a time sequential Sequential File Processing Sequential File Processing ­ Closing Sequential Files ­Form CLOSE or CLOSE #filenumber ­Purpose - terminates the association between a Purpose filenumber and a file that was established in a previously executed OPEN command previously ­If no file numbers follow the keyword CLOSE, If all opened files are closed all Sequential File Processing Sequential File Processing ­ Examples ­CLOSE #1, #2, #3 ­CLOSE #1 ­CLOSE #2, #1 ­CLOSE Summarization of Rules of the Summarization of Rules of the CLOSE statement ­ A file must be opened before it can be file closed closed The PRINT #n Statement The PRINT #n Statement ­ General Form: ­PRINT #n, item pm item pm…pm item n is a filenumber assigned to a file defined in an is OPEN statement OPEN ­ item is a constant, variable, expression, function item references or null references ­ pm is a comma or semicolon ­ ­ Purpose - writes information to a sequential Purpose file in auxiliary storage file The PRINT #n Statement The PRINT #n Statement ­ Examples ­PRINT #1 ­PRINT #2, Emp.Name$, Age, Weight ­PRINT #1, “Total Sales =====>”; Total ­PRINT #2, X + Y/4, C * 8 ­PRINT #3, Q1.Tax, Q2.Tax, Q3.Tax, Q4.Tax ­PRINT #2, Sum; The PRINT #n, USING The PRINT #n, USING STATEMENT ­ General Form ­PRINT #n, USING string expression; list n is a filenumber assigned to a file defined in an is OPEN statement OPEN ­ string expression is a string constant or a string string variable variable ­ list is a list of items to be displayed in the format list specified by the format field specified ­ The PRINT #n, USING The PRINT #n, USING STATEMENT ­ Purpose - Provides for controlling exactly Purpose the format of a program’s output to a sequential file in auxiliary storage by specifying an image to which that output must conform must The PRINT #n, USING The PRINT #n, USING STATEMENT ­ Examples ­PRINT #3, USING “The answer is #,###.##”; Cost ­PRINT #2, USING “## divided by # is #.3”; Num; Den; PRINT Quot Quot ­T1$ = “Total cost ====> $#,###.##-” ­PRINT #7, USING T1$; Total ­PRINT #2, USING “\ \”; Cust,.Name$ ­PRINT #4, USING “Example _##”; NUMBER ­PRINT #9, USING “#.##^^^^^”; Dis.1; Dis.,2, Dis.3, Dis.4 Flowchart of the OPEN and CLOSE Flowchart of the OPEN and CLOSE Statements Open for Output Open as filenumber 1, REPORT.lib REPORT.lib Close REPORT.LIB Writing Data to a Sequential File Writing Data to a Sequential File ­ WRITE #1, Stock$, Location $, Desc$, WRITE Cost, Price, Quantity Cost, The WRITE #n Statement The WRITE #n Statement ­ General Form ­WRITE #n; list of variable ­n is a filenumber assigned to a sequential file is opened for output opened ­ Purpose - writes data items separated by Purpose commas to a sequential file in auxiliary storage storage The WRITE #n Statement The WRITE #n Statement ­ Examples ­WRITE #1, Cost, Margin, Price ­WRITE #2, Amount, Description$ ­WRITE #3, Dependents, Tax The INPUT #n Statement The INPUT #n Statement ­ For data to be read from a sequential file, For the following must be true: the ­The file must already exist ­The file must be opened for input ­The data items in the file must be separated by The a comma or by a carriage return character <cr> comma The INPUT #n Statement The INPUT #n Statement ­ General Form ­INPUT #n, list of variables ­n is a filename assigned to an existing is sequential file opened for input sequential The INPUT #n Statement The INPUT #n Statement ­ Purpose - Reads data items from a Purpose sequential file in auxiliary storage and assigns them to variables assigns ­ Examples ­INPUT #1, Sum, Fix, Desc$, Price ­INPUT #3, Amount ­INPUT #2, Code$, Salary, Tax, Dependents The EOF Function The EOF Function ­ When a sequential file that was opened for When output is closed, the PC automatically adds an end-of-file mark after the last record end-of-file written to the file written The EOF Function Example: The EOF Function Example: ­ OPEN “INVENTORY.DAT” FOR INPUT OPEN AS # AS ­ DO WHILE NOT EOF (1) ­INPUT #1, Stock$, Location$, Des$, Cost, INPUT Price, Quantity Price, ­GOSUB Compute.And.Accumulate ­GOSUB Print.Record ­ LOOP Paging a Report Paging a Report ­ Paging - in multiple-page reports, the report Paging title, the column headings, and a page number should be printed at the top of every page every Paging a Report Paging a Report ­ A line counter should be incremented to line keep track of what line the printer is on keep Paging a Report: The logic for single­ Paging a Report: The logic for single­ spacing detail lines in a report ­ If the line counter is greater than or equal to If the maximum lines per page, call the Print Report and Column Headings module Report ­ Print the Detail line ­ Increment the line counter by 1 Increment ­by 2 for double-spacing or by 3 for triple by spacing spacing Paging a Report: Logic a for the Print Paging a Report: Logic a for the Print Report and Column Headings Modules ­ Increment the page counter by 1 ­ Advance the paper to the top of the page ­ Print the report title, column headings, and Print page counter page ­ Set the line counter equal to the number of Set lines printed in this module plus 1 lines Paging a Report: Paging a Report: Characteristics of Printers ­ The paper in a printer is normally 8.5 inches The wide and 11 inches long on continuous forms (Dot matrix) forms ­ Printers Print 6 or 8 lines per inch - the Printers default value is 6 lines per inch default Paging a Report: Paging a Report: Characteristics of Printers ­ ­ When the printer is turned on, it establishes as the top of When the page the line on the paper that the print-head mechanism (or the ribbon) rests on. To ensure that the paper is aligned properly for a program, it is good practice to request in the initialization module that the user align the paper to the top of the page the To align the paper, turn the printer off and use the platen To knob to advance the paper so the perforation between sheets is approximately one inch above the top of the printsheets head mechanism Paging a Report: Paging a Report: Characteristics of Printers ­ In the Print Report and Column Headings In module, instruct the printer to advance to the top of the page by printing a Form Feed character ­LPRINT CHR$(12); ‘ Advance the paper to top LPRINT of page of Control­Break Processing Control­Break Processing Programs that are written to generate levels of subtotals Programs use a technique involving control fields and control breaks breaks ­ Control field - contains data that is to be compared Control from record to record from ­ Control break - occurs when the data in the control Control field changes field ­ Control variable - the variable assigned to the selected Control data data ­ Summary Slide Summary Slide ­ Introduction ­ Data Files ­ QBASIC File handling Statements ­ File Organization ­ Sequential File Processing ­ OPEN Statement for Sequential Files ­ Summarization of Rules of the OPEN statement Summary Slide (cont.) Summary Slide (cont.) ­ Sequential File Processing ­ Summarization of Rules of the CLOSE Summarization statement statement ­ The PRINT #n Statement ­ Flowchart of the OPEN and CLOSE Flowchart Statements Statements ­ Writing Data to a Sequential File ­ The WRITE #n Statement Summary Slide (cont.) Summary Slide (cont.) The INPUT #n Statement ­ The EOF Function ­ The EOF Function Example ­ Paging a Report ­ Paging a Report: The logic for single-spacing Paging detail lines in a report detail ­ Paging a Report: Characteristics of Printers ­ Control-Break Processing ­ ...
View Full Document

This note was uploaded on 06/06/2011 for the course MIS 105 taught by Professor Grigoletti during the Spring '11 term at Moraine Valley Community College.

Ask a homework question - tutors are online