27 April, 2000 CS1001 Lecture 25 Files Internal Files.

Slides:



Advertisements
Similar presentations
Input and Output READ WRITE OPEN. FORMAT statement Format statements allow you to control how data are read or written. Some simple examples: Int=2; real=
Advertisements

1 Basic I/O Input and Output. 2 The READ Statement Basic Version l Performs a list-directed read from the file (or device) indicated by the unit number.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Component 9, Slide 1 CP2030 Copyright © University of Wolverhampton CP2030 Visual Basic for C++ Programmers v Component 9 working with text files and random.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Reading and Writing Files Keeping Data. Why do we use files? ä For permanently storing data. ä For dealing with information too large to fit in memory.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Input and output. Input streams n there are two ways of handling I/o n list-directed –done using default settings –PRINT*, num –READ*, num n formatted.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Chapter 5 Input / Output. 2 Control over input & output  The input and output are basically facilitates a communication between the user and the program.
Guide To UNIX Using Linux Third Edition
Understanding the Mainline Logical Flow Through a Program (continued)
Guide To UNIX Using Linux Third Edition
CHAPTER 6 FILE PROCESSING. 2 Introduction  The most convenient way to process involving large data sets is to store them into a file for later processing.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
21 April, 2000 CS1001 Lecture 27 Final Examination Review.
Chapter 6 AN INTRODUCTION TO FILES AND FILE PROCESSING Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Structured COBOL Programming, Stern & Stern, 9th Edition
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Generalized I/O. Mystery of stars I/O statements took the form read *, x, y,z print *, a,b,c Such I/O is said to be List-directed or in free format user.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
1 Chapter 9 Writing, Testing, and Debugging Access Applications.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
Indexed and Relative File Processing
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
File IO and command line input CSE 2451 Rong Shi.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Shell Scripting – Putting it All Together. Agenda Escaping Characters Wildcards Redirecting Output Chaining and Conditional Chaining Unnamed and Named.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
21 April, 2000 CS1001 Lecture 22 Formatted Input and Output.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
19 - 2/25/2000AME 150L1 Solving Systems of Linear Equations.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 2 (Tuesday)
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Fortran: Control Structures Session Three ICoCSIS.
Learners Support Publications Working with Files.
Files A collection of related data treated as a unit. Two types Text
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Introduction to C CSE 2031 Fall /3/ :33 AM.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Binary Files.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Introduction to C++ Programming
Iteration: Beyond the Basic PERFORM
Topics Introduction to File Input and Output
Chapter 8 - Visual Basic Schneider
Topics Introduction to File Input and Output
Week 10 FILE PROCESSING.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Presentation transcript:

27 April, 2000 CS1001 Lecture 25 Files Internal Files

27 April, 2000 File Access OPEN the file READ from an existing file WRITE to a new file CLOSE the file

27 April, 2000 OPEN Statement OPEN (open-list) where open-list is: Must include Unit Specifier Also includes: –FILE = Clause –STATUS = Clause –ACTION = Clause May include –POSITION = Clause – IOSTAT = Clause May include : –ERR = Clause –ACCESS = Clause –FORM = Clause –RECL = Clause –BLANK = Clause –PAD = Clause –DELIM = Clause

27 April, 2000 Unit Specifier Required –May not be 0, 5, or 6 –0 is standard error output, mapped to unit 6 –5 is standard input, your keyboard –6 is standard output, your monitor –Has the form UNIT = integer-expression or simply integer-expression FILE = Clause FILE = character-expression, where character- expression is the filename e.g., FILE = “file.out”

27 April, 2000 STATUS = Clause STATUS = character-expression, where character- expression can have one of the following values: –OLD meaning the file already exists –NEW meaning the file is being created –REPLACE creates a new file to replace an old file –SCRATCH no name for FILE= given, creates a work file that is deleted upon CLOSE –UNKNOWN default if STATUS= omitted e.g., STATUS = “NEW”

27 April, 2000 IOSTAT = Clause IOSTAT = status-variable, where status-variable is an integer variable to which is assigned: –0 if the file is opened successfully –Positive value otherwise, representing the number of the error message in a list found in the system manuals e.g., IOSTAT = iOpenError then later on, check iOpenError

27 April, 2000 ERR = Clause ERR = n, where n is the label of an executable statement that is the next statement executed if an error occurs in trying to open the file e.g., ERR = PRINT *, “File Open Error = “, OpenError

27 April, 2000 READ Statement READ (control-list) input-list –Must have a Unit Specifier –Must have a Format Specifier –Optional ADVANCE =Clause –Optional IOSTAT = Clause –Optional END = Clause –Optional ERR = Clause, same as OPEN –Optional REC = Clause for direct-access files e.g., READ (12, 10) iVariable, rVariable, cVariable 10 FORMAT (I4, F7.2, A12)

27 April, 2000 END = Clause END = n, where n is the label of a statement to be executed when the end of a sequential file is encountered e.g., END = PRINT *, “End of file reached”

27 April, 2000 Comment on File Reading In a file like “student.dat” on page A17, to read in a last name, like “Peters”, you must read in: –All of the lines (records) up to that line –All of the line itself READ (12, 10) iStudNum, cLName, cName, cInit, & cAddress, iPhone, cGender, iClass, cMajor, iCredits, & iGPA 10 FORMAT (I5, A15, A15, A1, A22, I7, A1, I1, A4, I3, I3)

27 April, 2000 WRITE Statement WRITE (control-list) output-list –Must have a Unit Specifier –Usually has a Format Specifier –Optional ERR = Clause –Optional IOSTAT = Clause –Optional REC = Clause e.g., WRITE (13, 10) iVar, rVar, cString 10 FORMAT (I4, F7.2, A12)

27 April, 2000 ENDFILE Statement ENDFILE writes into the file a special record called and end-of-file record When encountered by a READ statement: –IOSTAT = clause condition can be detected –END = clause statement can be executed Once encountered, no more data can be transferred to or from this file unless the file is repositioned using REWIND or BACKSPACE

27 April, 2000 CLOSE Statement CLOSE (close-list) where close-list is: –Must include Unit Specifier, same as OPEN –May include IOSTAT = Clause, same as OPEN –May include ERR = Clause, same as OPEN –May include STATUS = Clause –All files that are not closed by means of a CLOSE statement are automatically closed when an END or STOP statement is executed

27 April, 2000 SUBROUTINE ReadList (Names, Numbers, Active, NumRecords) INTEGER, INTENT(IN) :: NumRecords CHARACTER *20, INTENT(OUT) :: Names(NumRecords) CHARACTER *13, INTENT(OUT) :: Numbers(NumRecords) LOGICAL, INTENT(OUT) :: Active(NumRecords) CHARACTER *20 :: Filename INTEGER :: i,Status PRINT *, 'Enter filename of input file: ' READ *, Filename OPEN (UNIT=1, ACCESS = 'sequential', FILE = Filename, & FORM = 'Formatted', STATUS = ’old') DO i = 1, NumRecords READ (1, 2, IOSTAT=Status) Names(i), Numbers(i), Active(i) 2 FORMAT (A20, A13,L1) IF (Status <= 0 ) EXIT END DO ENFILE(1) CLOSE (1) END SUBROUTINE ReadList

27 April, 2000 Internal Files Sequence of memory locations containing information stored in character form and named by a character variable, array, or array element –Used to convert character information to numeric form –Used to convert character information into a character array Internal File Example Given cDate = “JULY 4, 1776” cYear = cDate(9:12) READ (UNIT = cYear, FMT = ‘(I4)’) iYear or READ (cYear, ‘(I4)’) iYear or READ (cDate(9:12), ‘(I4)’) iYear or READ (cDate, ‘(8X, I4)’) iYear Each of these takes the last four characters in the string cDate and converts it to an integer format named iYear

27 April, 2000 OPEN Arguments Back up slides

27 April, 2000 ACTION = Clause ACTION = i-o-action, where i-o-action is a character expression whose value is one of: –“READ” which opens the file for reading only –“WRITE” which opens the file for writing only –“READWRITE” which opens the file for reading and writing e.g., ACTION = “READ”

27 April, 2000 POSITION = Clause POSITION = character-expression, where the value of character-expression is: –“REWIND” which positions the file at its initial point (beginning), default for new file –“APPEND” which positions the file at its end –“ASIS” leaves its position unchanged, default for an existing file already open e.g., POSITION = “REWIND”

27 April, 2000 ACCESS = Clause ACCESS = access-method, where access-method can be either: –“SEQUENTIAL” meaning a file is accessed sequentially, without defined records –“DIRECT” for record oriented files, where each record has a record number for access e.g., ACCESS = ‘“SEQUENTIAL”

27 April, 2000 FORM = Clause FORM = form-specifier, where form-specifier is a character expression that is either: –“FORMATTED” –‘UNFORMATTED” –If omitted, file is assumed formatted if sequential, unformatted if direct-access e.g., FORM = “FORMATTED”

27 April, 2000 PAD = Clause Only applies to FORMATTED files PAD = character-expression, which can have the values: –“YES”, means the input proceeds as if the actual record were padded with blanks between fields –“NO” e.g., PAD =“YES” is the default

27 April, 2000 RECL = Clause RECL = record-length, where record-length is an integer whose value must be positive –Only used for direct-access files and specifies the length of the records in the file –In a formatted file, the record length is the number of characters in a record e.g., RECL = 25

27 April, 2000 BLANK = Clause Only applies to FORMATTED files BLANK = blank-specifier, which can be –“ZERO” –“NULL” The first causes blanks in numeric fields to be interpreted as zeros, the other causes blanks to be ignored. BLANK = “NULL” is default

27 April, 2000 DELIM = Clause Only applies to FORMATTED files DELIM = character-expression, which can be: –“APOSTROPHE” is to be used as the delimiter for character strings written to a file –“QUOTE” is to be used as the delimiter for character strings written to a file –“NONE” –DELIM = “NONE” is the default

27 April, 2000 STATUS = Clause STATUS = character-expression –“KEEP” may not be used for SCRATCH files. Otherwise the state of existence of the file remains unchanged (not deleted). –“DELETE” is the default if this clause is omitted, however, it depends upon a PERMANENCE property which can be “TEMPORARY” or “PERMANENT” –STATUS is usually not used

27 April, 2000 READ Arguments Backup slides

27 April, 2000 ADVANCE = Clause ADVANCE = character-expression, which can have the values: –“YES” specifies that output should advance to a new line after the current output has been completed –“NO” does not advance the output –ADVANCE = “YES” is the default

27 April, 2000 IOSTAT = Clause IOSTAT = integer-variable, where –Positive value if an error occurs, usually the error number from a list in a manual –Negative value if the end of data occurs but no input error occurs –0 if neither an input error nor an end of data occurs

27 April, 2000 REC = Clause REC = integer-expression, where integer- expression is positive and indicates the number of the record to be read from a direct-access file –Control list may not contain both an END = clause and a REC = clause e.g., REC = iPartNumber

27 April, 2000 WRITE arguments Backup slides

27 April, 2000 File-Positioning Statements REWIND unit or REWIND position-list BACKSPACE unit or BACKSPACE position-list ENDFILE unit or ENDFILE position-list Where position-list –Must contain unit or UNIT = unit –May contain ERR = clause –May contain IOSTAT = clause

27 April, 2000 REWIND and BACKSPACE REWIND positions the file at the beginning of the file’s first record BACKSPACE positions the file at the beginning of the preceding record or line Neither has an affect if the file is already at the beginning e.g., REWIND 8 or REWIND (8, IOSTAT = iError)

27 April, 2000 Not Needed For This Class INQUIRE Merging Files External Sorting Unformatted Files