BO65: PROGRAMMING WRITING TO TEXT FILES.

Slides:



Advertisements
Similar presentations
Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Advertisements

IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 9: Sequential Access Files and Printing
Introduction to a Programming Environment
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to.
Input/Output CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Computer and Programming File I/O File Input/Output Author: Chaiporn Jaikaeo, Jittat Fakcharoenphol Edited by Supaporn Erjongmanee Lecture 13.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
1 CS161 Introduction to Computer Science Topic #13.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
INPUT AND OUTPUT 1. OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class Files (next topic) Printing (later) 2.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
Dr. Tami Meredith Saint Mary's & Dalhousie Universities.
Files Tutor: You will need ….
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Files and Streams. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a permanent way to store.
B065: PROGRAMMING WRITING TO CSVFILES. Starter  Quick task:  Write a program which asks for the name and age of 3 students and writes these details.
Python: File Management Damian Gordon. File Management We’ve seen a range of variable types: – Integer Variables – Real Variables – Character Variables.
Unit 2 Technology Systems
Development Environment
Introduction to Computing Science and Programming I
PROGRAMMING ARRAYS.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
INPUT AND OUTPUT.
Files.
Variables, Expressions, and IO
CSCI 3327 Visual Basic Chapter 11: Files and Streams
Files and Streams.
Engineering Innovation Center
Starter Write a program that asks the user if it is raining today.
Files and Streams Lect3 CT1411.
Sequential Input and Output using Text Files
File Input/Output (I/O)
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Python I/O.
Number and String Operations
Interactive I/O Input from keyboard Must prompt user User friendly
Building Web Applications
Week 9 – Lesson 1 Arrays – Character Strings
Files & Streams.
Functions In Matlab.
Fundamentals of Data Structures
Introduction to TouchDevelop
Chapter 3.5 Input and Output
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
VB.Net Programming Console Application
Variables and Expressions
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
Text / Serial / Sequential Files
Files and Streams Lect10 GC201 12/1/2015.
Random Access Files / Direct Access Files
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
CIS16 Application Development and Programming using Visual Basic.net
B065: PROGRAMMING Variables 1.
User Input Keyboard input.
File Input and Output.
Files and Streams.
Starter Which of these inventions is: Used most by people in Britain
Chapter 11 Saving Data and Objects In Files
Text / Serial / Sequential Files
3.2 Working with Data Scope of variables 29/07/2019.
How to read from a file read, readline, reader
Getting Started With Coding
Presentation transcript:

BO65: PROGRAMMING WRITING TO TEXT FILES

Starter You are about to see a picture on screen. The picture will be displayed for 45 seconds. After the 45 seconds you will be given a piece of paper and you should try and re-draw what you have seen.

Starter Review You just used a similar concept of programming in that starter. You looked at something, created a space in your memory to remember parts. Yes, the computer is more accurate over time, but you both have one thing in common. If you don’t save (write down), you will forget a lot of what you have just seen.

Objectives Understand the different ways in which a computer can read and write data to file. Become familiar with the coding constructs of saving and reading data. Use saving and reading within your program.

The Concept So far, you have been writing programs that create and use variables and constants. These are in RUNTIME memory. When the program terminates, the contents of the variables are lost. In other words, the computer forgets! Imagine if it did this to a word processing document! To get around this, we can write our data to disk, and read it back when needed. There are a few ways we can do this.

File Types Text File (extension .txt). Basically a set of character values which are in a document line by line. You could open it in Notepad or similar application and read what was inside it. CSV File (extension: .csv). A comma-separated values file. Like a text file, with all related information on one line (e.g. all information about a customer: name, age, debt etc), with each field separated by commas. Binary file – (extension varies). A application, which is opened in say notepad would not be comprehensible to the human eye.

Preparing to Write Imports System.IO Your program needs instructions from a library. It needs to know HOW to read/write to files. At the top of your module write: Imports System.IO

Writing Console Entries to File 1 Add “Imports System.IO” to your General section. 2 Declare a channel for writing (add writing functionality using StreamWriter Dim fileWriter as StreamWriter 3 Specify the file you want to write to (in a variable or constant – useful to put at top of sub). Dim filename as string fileName = “c:\writerExample.txt 4 Bind the channel to the file (i.e. tell the computer what file you are going to write to, using your writing channel. fileWriter = New streamwriter(fileName) 5 Prompt the user to enter something in the console using the writeline command Store what the user types in a variable. E.g. strEntry = console.readline() 6 Write what was written in the console to file fileWriter.writeline(strEntry) 7 Close the file. fileWriter.close Let’s look at a coded example.

Reading the contents back in 1 Add “Imports System.IO” to your General section. 2 Declare a channel for reading(add writing functionality using StreamReader Dim fileReader as StreamReader 3 Specify the file you want to read from (in a variable or constant – useful to put at top of sub). Dim filename as string fileName = “c:\writerExample.txt 4 Bind the channel to the file (i.e. tell the computer what file you are going to write to, using your writing channel. fileReader = New streamReader(fileName) 5 Read each line from the file, until the file is empty. (Do Until FileReader.EndofStream)…Loop. Use FileReader.Readline() within the loop to read a line. e.g. strEntry = filereader.readline() 6 Display the contents of the variable on screen: console.writeline(strEntry) 7 Close the file. fileReader.close Let’s look at a coded example.

Objectives Review Understand the different ways in which a computer can read and write data to file. Become familiar with the coding constructs of saving and reading data. Use saving and reading within your program.

Plenary: What is the correct order? A. Add “Imports System.IO” to your General section. B. Bind the channel to the file (i.e. tell the computer what file you are going to write to, using your writing channel. fileWriter = New streamwriter(fileName) C. Prompt the user to enter something in the console using the writeline command Store what the user types in a variable. E.g. strEntry = console.readline() D. Declare a channel for writing (add writing functionality using StreamWriter Dim fileWriter as StreamWriter E. Write what was written in the console to file fileWriter.writeline(strEntry) F. Close the file. fileWriter.close G. Specify the file you want to write to (in a variable or constant – useful to put at top of sub). Dim filename as string fileName = “c:\writerExample.txt