WRITING TO AND READING FROM FILES MET 50. Files To read from a file, we still use the READ statement. To write output to a file, we use the WRITE statement.

Slides:



Advertisements
Similar presentations
Introduction to File I/O How to read & write data to a disk file...
Advertisements

Chapter 11 Introduction to Programming in C
1 Input/Output and Debugging  How to use IO Streams  How to debug programs  Help on coursework.
* 1 Common Dialog Control. * 2 You want your user to set property or provide your application with some information easily? How do you do it? The Common.
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=
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Fortran 4- Reading and Writing from Data Files Chapter 4 of your Fortran book.
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.
11/06/20151 FORTRAN 77 Programming. Lecture 5 : January 2001 Dr. Andrew Paul Myers.
ME1107 Computing Y Yan
MORE ON… MAKING YOUR OUTPUT LOOK NICE! MET 50. Pretty output Recall the more sophisticated PRINT statement: PRINT nn, GRAV nn FORMAT (stuff) We use the.
MAKING YOUR OUTPUT LOOK NICE! MET 50. The “IF” statement Suppose we have the statements: REAL, parameter :: GRAV = 9.81 PRINT*, ‘Value of GRAV is:’,GRAV.
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.
SUB-PROGRAMS MET 50. Using sub-programs The codes we/you have written so far are teeny ( lines). In science & engineering, many codes are huge.
CS201 – Expressions & I/O. Precedence What is the value of * 4 ? 20 or 14 ? Without parentheses, there are a set of rules to fall back on. Unary.
Formatted Output What we know: write(*,*) x print *, x General Form: write (unit, format, options) list unit = * : write to the monitor format = *: no.
FORTRAN Short Course Week 4 Kate Thayer-Calder March 10, 2009.
Chapter 5 Input / Output. 2 Control over input & output  The input and output are basically facilitates a communication between the user and the program.
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.
CHARACTER DATA MET 50. Character data We have extensively used REAL and INTEGER data types. There is also: CHARACTER data COMPLEX data LOGICAL data 12/1/2011.
Chapter 6 AN INTRODUCTION TO FILES AND FILE PROCESSING Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Game Programming © Wiley Publishing All Rights Reserved. The L Line The Express Line to Learning L Line L.
CCSA 221 Programming in C CHAPTER 2 SOME FUNDAMENTALS 1 ALHANOUF ALAMR.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
Python File Handling. In all the programs you have made so far when program is closed all the data is lost, but what if you want to keep the data to use.
Launch SpecE8 and React from GSS. You can use the chemical analyses in a GSS data sheet to set up and run SpecE8 and React calculations. Analysis → Launch…
CPSC Pascal Brent M. Dingle Texas A&M University Chapter 1.
Scientific Computing Division A tutorial Introduction to Fortran Siddhartha Ghosh Consulting Services Group.
Week 1 - Friday.  What did we talk about last time?  Our first Java program.
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.
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
Chapter 9 I/O Streams and Data Files
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.
Files Tutor: You will need ….
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
C OMPUTER P ROGRAMMING 1 Input and Variables. I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from.
The Development Process Compilation. Compilation - Dr. Craig A. Struble 2 Programming Process Problem Solving Phase We will spend significant time on.
Chapter 4 PROCEDURE DIVISION. Paragraphs PROCEDURE DIVISION divided into paragraphs Each is independent module or routine Made up of series of instructions.
CIT 590 Intro to Programming Files etc. Agenda Files Try catch except A module to read html off a remote website (only works sometimes)
LO: We’re learning to outline a program using Pseudo Code.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the differences between text and binary files ❏ To write programs.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
CIT 590 Intro to Programming Lecture 6. Vote in the doodle poll so we can use some fancy algorithm to pair you up You.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
Bbc microbit Lesson 3 – Temperature hot medium.
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 2: Getting Data into SAS
Variables, Expressions, and IO
ECONOMETRICS ii – spring 2018
Binary Files.
Python I/O.
Matlab review Matlab is a numerical analysis system
HOW TO CREATE A CLASS Steps:
Topics Introduction to File Input and Output
Running a Java Program using Blue Jay.
Week 10 FILE PROCESSING.
L L Line CSE 420 Computer Games Lecture #3 Introduction to Python.
Presentation transcript:

WRITING TO AND READING FROM FILES MET 50

Files To read from a file, we still use the READ statement. To write output to a file, we use the WRITE statement. 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 2

Files WRITE (mm, nn) (things to write) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 3 Tells us the FORMAT of what we write Tells us where we write the output to

Files READ (mm, nn) (things to write) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 4 Tells us the FORMAT of what we read Tells us where we read the input from

Files Example… WRITE (25, 16) X1, X2, X3 16 FORMAT (1X, 3F10.2) Also… WRITE (mm, nn) X1, X2, X3 “mm” and “nn” must be integer WRITE (*, nn) directs output to screen 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 5

Files Guidance for values of “mm” … In the old days, WRITE (6, nn) - directed output to the printer READ (5, nn) - read input from the screen Caution: the values ‘5’ and ‘6’ might be hard-wired into old code. 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 6

Files Files… A file is like a book. It needs to be opened, closed, flipped thru etc. We have commands for this! 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 7

Files Opening a file… OPEN (UNIT=mm, FILE=“name of file”, STATUS=“OLD or NEW or replace”, ACTION=“READ or WRITE or READWRITE”, POSITION=“REWIND or APPEND or ASIS”, IOSTAT=integer number) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 8

Files Example… OPEN (UNIT=12, don’t need to write “UNIT” FILE=“temperature_data_1977.dat”, name STATUS=“OLD”, file already exists ACTION=“READWRITE”, usually omit POSITION=“ASIS”,often omit IOSTAT=integer number) see later… 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 9

Files Simplified example… OPEN (12, FILE=“temperature_data_1977.dat”, STATUS=“OLD”, IOSTAT=IOS) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 10

Files Code example…running this… REAL :: DATE, TEMP add code here to generate some data to write! OPEN (12, FILE=“temperature_data_1977.dat”, STATUS=“NEW”, IOSTAT=IOS) WRITE (12,22) DATE, TEMP 22 FORMAT (1X, 2F12.4) After you compile and run this code, you would find that the file “temperature_data_1977.dat” has appeared in your directory! 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 11

Files And reading from a file? REAL :: DATE, TEMP OPEN (12, FILE=“temperature_data_1977.dat”, STATUS=“OLD”, IOSTAT=IOS) READ (12,22) DATE, TEMP 22 FORMAT (1X, 2F12.4) IF (IOS > 0) THEN PRINT*,’Bad data or missing data with IOS=‘, IOS ENDIF add some code to do something with this data!!! 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 12

Files Two three more things… (1) REWIND command Sometimes we may need to : OPEN a file READ the data READ it all again from the start 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 13

Files Code could include these lines… OPEN (12, file=‘name.dat’, status=‘OLD’) READ (12,100) X,Y,Z 12 FORMAT (bla bla bla) do some calculations REWIND (12) READ (12,100) X,Y,Z do some more calculations CLOSE (12) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 14

Files (2) Forms in which data is stored Either:text data Or:binary data 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 15

Files text data Looks just like regular characters on the screen IS readable! Read: Example…online (/121) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 16

Files binary data Looks unintelligible Is NOT! Example…online (/121) 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 17

Files (3) Reading & writing binary data This is NOT formatted! Space-saving solution for massive datasets. 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 18

Files Code would include lines like this… OPEN (12, file=‘name.dat’, status=‘OLD’, form=‘unformatted’) READ (12) X,Y,Z note no FORMAT statement! and WRITE (12) X,Y,Zditto! 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 19

Files Large datasets often arrive in binary form. A new paradigm in atmospheric science is “netCDF” 10/13/2011 MET 50, FALL 2011, CHAPTER 5 PART 2 20