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.

Slides:



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

File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
CSM18 FilesDr Terry Hinton1 VB - Persistence in software Information entered into a program is volatile Can switch computer off Power can be removed accidentally.
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=
P1PMF Split1 QBASIC. P1PMF Split2QBasic Command Prompt Will launch the emulator DOS operating system? Press Alt + Enter to display the widescreen.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Introduction to C Programming
COMP234 Perl Printing Special Quotes File Handling.
File Handling Advanced Higher Programming. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a.
Files Organisation sequential files. Readings u Schneider Chapter 8 u Shelly Cashman to 9.14; to 9.11 u Meyer to 2-37; 1995.
Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
Component 9, Slide 1 CP2030 Copyright © University of Wolverhampton CP2030 Visual Basic for C++ Programmers v Component 9 working with text files and random.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
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.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Introduction to C Programming
Introduction to Help Text Online help to explain your program’s operation “Splash screen” Providing simple to understand prompts in dialog boxes Handling.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS0004: Introduction to Programming Variables – Strings.
Tutorial 61 List Box Control Can be used to display a set of choices from which the user can select only one You also can create multi-selection list boxes.
Creating Sequential Files, Adding Data, & Deleting Data.
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
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.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
© 1999, by Que Education and Training, Chapter 9, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
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.
Chapter 5 Files and Exceptions I. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A.
READING AND WRITING FILES. READING AND WRITING FILES SEQUENTIALLY  Two ways to read and write files  Sequentially and RA (Random Access  Sequential.
Files Tutor: You will need ….
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Python Let’s get started!.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
Compunet Corporation1 Programming with Visual Basic.NET Input and Output Files Lecture # 6 Tariq Ibn Aziz.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 9 Structures and Sequential Access Files.
Learners Support Publications Working with Files.
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.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
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.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Microsoft Visual Basic 2005: Reloaded Second Edition
Python Let’s get started!.
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Binary Files.
File Handling Programming Guides.
Topics Introduction to File Input and Output
Introduction to C++ Programming
Chapter 14 - Advanced C Topics
Using files Taken from notes by Dr. Neil Moore
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Visual Basic 6 Programming.
Text / Serial / Sequential Files
Random Access Files / Direct Access Files
Topics Introduction to File Input and Output
EECE.2160 ECE Application Programming
Reading from and Writing to Files
Topics Introduction to File Input and Output
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
Text / Serial / Sequential Files
Reading from and Writing to Files
Presentation transcript:

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.

Sequential Access Files ä Think of files as being stored on tape. ä Sequential Access means you must pass over all the information in a file to get the stuff you want.

Illustration F1F2F4F3File 5F6File 7 To get from here...To here... We have to read all this. Read Head Tape Moves

Random Access Files ä Think of files stored on a disk. ä Random Access means you can have any information at any time. (Like in RAM!)

Illustration File One File Two Move the head from one file to another ignoring everything in between.

Random Access Files, Details ä WE WILL NOT COVER THESE IN THIS CLASS ä These files use indexes to locate information within them. ä It means dealing with the indexes, how the disk memory is used, special ways to delete files …..

Opening Files ä Questions to Answer: ä Do you want to read from it, write to it, both? ä Does the file already exist? ä What shall we do with the file that already exists? ä Are we looking at binary data or text data? ä Where is the file?

File Opening ä A file must first be opened before it can be used. ä Opening a file does two things: ä Makes sure its OK to open the file ä Assigns an identifier to the file. ä Files stay open until we close them (or the program ends)

OK to Open File? ä You haven’t opened it already ä You aren’t violating the file’s integrity ä Somebody else has it open… ä You’re already using for a different purpose.

Assigning an Identifier ä Allows us to identify which file to write to and read from. ä Useful if Multiple Files are Open

The Open Statement ä Open file For Mode As filenumber ä file is the Full pathname of the file to be opened. ä Mode is how the file will be used. ä filenumber is the number that identifies the file.

Modes ä Input ä Sequential access for reading, starts at beginning ä Output ä Sequential access for writing, erases old file and starts at beginning ä Append ä Sequential access - doesn’t erase the file, starts writing at the end.

Two Modes we Won’t Use ä Random ä Which we won’t use in this class - This is the default! ä Binary ä Which we also won’t use. Opens binary files.

The Open Statement, Con’t ä Opening a non-existent file for Input causes an Error! ä We’ll write a special function for handling this. ä Opening a non-existent file for Output causes it to be created. ä Opening an existing file for Output causes it to be erased. ä Opening an existing file for Append causes data to be entered at the end of the existing file.

Output to a File ä Write # filenum, variablelist ä filenum is the file identifying number ä variable list is a list of variables. ä This is the easiest file output function. ä Use it unless you are forced to use something else.

Format ä These are written out using commas between values and double quotes around strings. ä “Here is a string”,19,27.3,”String” ä “More strings”,20,15.2,”More Text”

Illustration of File Writing Dim fleeb As String, norb As Single Dim ski As Integer, i As Integer fleeb = “Test1” norb = 98.6 ski = 100 Open “file.txt” For Output As 1 Write #1, fleeb, norb, ski Close 1 “Test1”,98.6,100 file.txt

Printing to a File ä Print # filenum, var ; var, var ; var ä filenumber is from the Open Statement ä var can be ä the function Spc(n), for n spaces, ä Tab(n) for nth column tab, ä a variable ä the semicolon puts no space between items ä the comma prints items with spaces.

Input From a File ä Input # filenumber, variablelist ä filenumber is the file to read from ä Identified in the open statement. ä variables list is the list of variables to read the value into. ä numbers get numbers, strings get either double quoted strings or first word. ä Use this if at all possible.

Illustration of File Reading Dim fleeb(1 To 3) As String, Dim norb(1 To 3) As Single Dim ski(1 To 3) As Integer Dim i As Integer Open “file.txt” For Input As 1 For i = 1 to 3 Input #1, fleeb(i), norb(i),ski(i) Input #1, fleeb(i), norb(i),ski(i) Next i Close 1 “Test1”,98.6,100 “Test 2”,93.1,100 “Quiz 3”,18,25 file.txt fleeb norb ski

Input From a File, Con’t ä Input (n,filenumber) ä n is the number of characters to read in. ä filenumber is the file to read from. ä This was identified in the Open statement. ä This is useful if you must read in a file created with the Print# function.

Close File Statement ä Close filenumber(s) ä File identification number or numbers. ä Closes file and removes number assignment to it. ä Will “rewind” sequential files to beginning.

For i = 1 to 10 Open “f” for Input As 1 Input#, a(i), b(i) Close 1 Next i Good Thing Bad Thing Closing a File Open “f” for Input As 1 For i = 1 to 10 Input#, a(i), b(i) Next i Close 1

Formatting Data ä Write Format must be the same as Read Format to ensure that we have data integrity. ä Be very careful of this.

File Length ä We don’t always know the file length ä We can use the EOF(filenumber) function. ä filenumber is the number you assigned to the file. ä EOF returns True at the File end. ä EOF returns False otherwise.

Reading an unknown length file. Open “file.txt” For Input As 1 While(Not EOF(1)) Input#1, fred, wilma Input#1, fred, wilmaWend Close 1

FreeFile Function ä A useful function when you’re opening a lot of files and you’re not sure what number you want to assign to each. ä Identifies a number which has not yet been assigned to a file.

Input Files that Don’t Exist ä Requires us to use the error handling VB provides. ä We’ll put this in a function so that it doesn’t cause problems. ä When an Error occurs in VB it looks for an error handler. If none is found, the program ends. (Bad Thing)

Using the Error Handler ä We can create an error handler by inserting the statement: ä On Error Goto label ä Where label is a word that occurs later in the code with a comma after it. ä Special rules apply to these error handlers.

Comments ä We’ll tuck our error handler away in a function so that its use will be easy to control. ä This function will determine if a file exists. ä If it does it returns true ä Otherwise it returns false.

File_Exists Function Function File_Exists (FileName as String) As Integer On Error Goto File_error Open FileName For Input As 1 Open FileName For Input As 1 Close 1‘If it reaches this line Close 1‘If it reaches this line File_Exists = True‘the file was found File_Exists = True‘the file was found Exit Function Exit Function File_error:‘If it wasn’t found, control is File_Exists = False‘here via the On Error line. File_Exists = False‘here via the On Error line. Exit Function Exit Function End Function