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=4.5678.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

CHAPTER 5 INPUT/OUT FORMAT. Introduction READ (*,*) x WRITE (*,*) x This is free format: the first * is for I/O device number (* = input is keyboard *
1 Chapter 2 Basic Elements of Fortran Programming.
Fortran 4- Reading and Writing from Data Files Chapter 4 of your Fortran book.
Introduction to C Programming
11/06/20151 FORTRAN 77 Programming. Lecture 5 : January 2001 Dr. Andrew Paul Myers.
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.
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.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
27 April, 2000 CS1001 Lecture 25 Files Internal Files.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Introduction to a Programming Environment
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.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
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.
Input and Output in Console Mode UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Homework Reading Programming Assignments
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
CIS Computer Programming Logic
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
How to Write a Fortran Jiffy (C)2005 Mark Rould University of Vermont.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
INPUT / OUTPUT STATEMENTS
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
21 April, 2000 CS1001 Lecture 22 Formatted Input and Output.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
© 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.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
19 - 2/25/2000AME 150L1 Solving Systems of Linear Equations.
Fortran: Control Structures Session Three ICoCSIS.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CSCE Do Loops Simplest form DO j = 1,100 PRINT *,j END DO Variable j runs from 1 to 100 counting by ones.
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 ,
C Formatted Input/Output
Programming Fundamentals
MATLAB: Structures and File I/O
Chapter 2 part #3 C++ Input / Output
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.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 11 Introduction to Programming in C
Topics Introduction to File Input and Output
Introduction to CS Your First C Programs
Introduction to C++ Programming
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Input and Output.
Homework Reading Programming Assignments Finish K&R Chapter 1
Chapter 2 part #3 C++ Input / Output
Topics Introduction to File Input and Output
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
Week 10 FILE PROCESSING.
OUTPUT DESIGN PRINT K, expression list K FORMAT (specification list)
Presentation transcript:

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= PRINT ‘(I6,2x,F5.1)’,int,real. This will print the following on the screen: (i)Integer in a field of 6 (here 5 spaces + 2) (ii)Two blanks (2x) (iii)Real in a field of 5 with 1 digit after the decimal pt

FORMAT statement again You can define a format either directly by means of: ‘(format-instructions)’ or “(format-instructions)” Or by means of a FORMAT statement (useful if you want to use the same format in several PRINT or WRITE statements. PRINT 30, int1, int2, int3, real1, real2, real3 30FORMAT(1x,3i8,3f10.3) The 30 is a STATEMENT LABEL

Types of format descriptors Iw Integer of width w Fw.dReal of width w with d characters after the. OwOctal of width w (write out no. as octal) ZwHexadecimal of width w Ew.dExponential format e.g e-09. W is the total no. chars, d the no. after the decimal point A or AwCharacter (use A for input and Aw for output) There are others you may need later on, e.g Lw for logical

Example 1 Print ‘(1x, “x=”,f6.2, “ Y=”, e12.3)’,x,y Will print out x as a normal real and y in exponential format with 3 digits after the decimal point The format descriptor nx inserts n blanks The format descriptor tn tabs to the nth character, e.g.: Print 75, “John Q. Doe”, ‘CPSC’, Number 75 FORMAT(1x, A11, T16, A4, 2x, I3) Will produce the output > John Q. Doe CPSC 141

Example 2 A number BEFORE the format descriptor tells the program to repeat it. Thus 5I6means 5 integers of 6 fields 8e10.3means 8 exponential numbers with 3 digits after the decimal point 5(I8,2x,f10.3) repeats the pattern within the brackets Use of a slash within a Format statement forces input or output from a fresh line, e.g. Print ‘(2i3/a20)’,int1,int2,characters puts the characters on a fresh line

Formated READ This is normally used when reading from a file, but will work from the screen also: READ ‘(format-descriptor)’,var1,char1,var2,char2 Note that FORTRAN ignores blanks when reading in variables under format control – so that, for instance, with a format statement i6 the following all get read as 2: 2_____, _2____, __2___, ___2__, ____2_, _____2

Reading character input This is slightly tricky. Some compilers will allow: Character(len=7)::name Read*,name But in general they don’t. You need to use an A format: Read ‘(a)’,name Note that it is not necessary to define the length of the character field – the computer will read in however many characters it needs to fill the declared variable.

File processing: Open Before using a file you need an OPEN statement. This has many options, but you don’t need to know them all now. OPEN(unit,file=file_name,status=‘XXX’, action=‘YYY’) Unit is a NUMBER which identifies this file with a channel that can subsequently be accessed by PRINT, WRITE, READ File_name is the name of the file Status takes values: “OLD”,”NEW”,”REPLACE” or “UNKNOWN”. Action takes values ‘READ’, ‘WRITE’, OR ‘READWRITE’

Examples of Open Open(8,file=‘datasub/file1’) Opens file1 on channel 8 with unknown status, and readwrite access Character(len=20)::filename Filename=‘datasub/file1’ Open(13,file=filename,status=‘old’,action=‘read’) Opens file1 on channel 13 with status old, read only Open(22,file=‘newfile’,status=‘new’,action=‘write’) Creates a blank file called newfile in the current directory, with write access only Avoid using channels 5 or 6 as these are by default the keyboard and screen respectively

Reading from a file Very similar to reading from the screen, except you define the channel: Real,dimension(100)::indata Open(8,file=‘mydatafile’,status=‘old’) Read(8,*) i1,i2,i3,i4 Read in free-format Read(8,’(4i6)’)i1,i2,i3,i4Read under format control Read(8,30)i1,i2,i3,i4 Read(8,’(8f10.3)’)indataReads file directly into array Read(8,’(8f10.3)’)(indata(I),I=1,100)

Writing to a file The Write command is used to write data to a file, rather then the PRINT command. WRITE is very similar to READ: Write(8,*) x1,x2,x3,x4 writes in free format Write(8,’(2f10.3,5x,2e12.5)’)x1,x2,x3,x4 writes under format control Write(8,’(4f10.3)’)x writes out an array Write(8,’(4f10.3)’)(x(j),j=1,4)

Other file commands Close(8) closes a file (releasing the unit no. for a subsequent open command Rewind 8 goes back to the beginning of a file Backspace 8goes back one record. Useful if you’re not sure what kind of data to expect – read the line in as characters, decide what it is then backspace and read again as numbers

Error codes What if you read a file and reach the end of the data? Any further read statement causes the program to crash. It is advisable therefore to allow for this with IOSTAT: Integer :: test Do Read(8,900,iostat=test)x1,x2,x3,x4 If(test.eq.0)then …….. !No problem. Carry on executing program Else if (test.lt.0) then Exit !End of data. Leave do loop Else Print*,’error on reading file’;stop !stop executing program End do

Unformatted read/write Binary (or unformatted) files are much more compact than text (or ascii) files and for very large data files these are preferred. Reading in a binary file is quite easy: READ(8,iostat=test)array - there is no format statement However, you can only read data easily in this way if you used the corresponding write statement to create the file on the same computer (or at least type of computer) – since this maps the file directly to the memory of the computer.

Exercise 1 Write a program to write all the numbers from 1 to 100 into a file. Choose a sensible format Write a program to read this file, multiply each number by pi and write the results out to a new file Write a program to read your name and address from a file and write it to the screen in a different format.

Exercise 2 Write a program to read the data files you created in exercise 1 and convert the numbers from decimal degrees to degrees, minutes and seconds. Put the results into a new file with suitable explanatory text.