File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.

Slides:



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

L which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? l how is a string assigned.
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.
Last Week Looping though lists Looping using range While loops.
Lecture Roger Sutton CO331 Visual Programming 19: Simple file i/o Exceptions – Error handling 1.
Component 9, Slide 1 CP2030 Copyright © University of Wolverhampton CP2030 Visual Basic for C++ Programmers v Component 9 working with text files and random.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 9: Sequential Access Files and Printing
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Chapter 9 Files I/O: Files, Records and Fields. Basics of File Input and Output Have created both input and outputs from programs. Persistent data: What.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
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.
Computer and Programming File I/O File Input/Output Author: Chaiporn Jaikaeo, Jittat Fakcharoenphol Edited by Supaporn Erjongmanee Lecture 13.
Lecture Set 12 Sequential Files and Structures Part B – Reading and Writing Sequential Files.
Visual C Sharp – File I/O - 1 Variables and arrays only temporary - lost when a program terminates Files used for long term storage (Data bases considered.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
Guide to Programming with Python Chapter Seven (Part 1) Files and Exceptions: The Trivia Challenge Game.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
File I/O 11_file_processing.ppt
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
File I/O What We’ll Cover –Visual Basic Techniques for Text Files –.NET Techniques for Text Files What We’ll Not Cover –Binary File I/O –XML File I/O.
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.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
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.
Files Tutor: You will need ….
Last Week Lists List methods Nested Lists Looping through lists using for loops.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
Compunet Corporation1 Programming with Visual Basic.NET Input and Output Files Lecture # 6 Tariq Ibn Aziz.
Lecture 14 Arguments, Classes and Files. Arguments.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
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.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
1 CS101 Fall 2001 Lecture 1 In order to write a program, you must first telnet to your pegasus account and login either from a Rutgers computer in a lab,
Input/Output Chapters 8 & 9. Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both.
Python focus – files The open keyword returns a file object Opening a file myFile = open('C:\file.txt', arg) Optional argument The second argument controls.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
INPUT AND OUTPUT.
Reading & writing to files
Files and Streams.
Files and Streams Lect3 CT1411.
File Input/Output (I/O)
Chapter 3.5 Input and Output
Tutorial 9 Sequential Access Files and Printing
Files and Streams Lect10 GC201 12/1/2015.
CIS16 Application Development and Programming using Visual Basic.net
BO65: PROGRAMMING WRITING TO TEXT FILES.
Introduction to Programming Lecture 6
Files and Streams.
Presentation transcript:

File IO

 File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files

 Significant programs will require you to read/write data from data files  Configuration files  Data Files  For complex applications you will use Databases  Microsoft SQL Server/Access  We will only concentrate on simple text files

 Simple text files are nothing more than a series of characters Hello there. This is text in a text file This is a second line in the same text file.  At the end of the file there is a character called EOF (End Of File). You can’t see it  But you can test for it!!

 To use a file, you must:  Include a special Imports statement at the top of your program: Imports System.IO  Open the file  If the file doesn’t exist, you can’t open it  Then you must create it  Data can be written to the file (always at the end of the file!!)  Data can be read from the end of the file  Files can have Random Access to write/read from any point of the file – beyond the scope of this class  These are called Binary files.  Can’t view them in an editor!!  When finished with the file, close it!!!

 A Visual Basic Object that you declare (Dim) so that you can write to a file Dim filMyFile As StreamWriter  To create a new text file, use File.CreateText filMyFile = File.CreateText(“myfile.txt”)  To add to an existing file, use AppendText filMyFile = File.AppendText(“myfile.txt”)

 Once the file is open, you write to it with the Write / WriteLine statements filMyFile.WriteLine(“This is a line of text!!”)  The Write statement does NOT add a newline after the text (does not press the return key)  The WriteLine statement adds a newline after the text  To put a blank line in the file, just add a WRiteLine without text filMyFile.WriteLine(“”)

 You must always close files when finished with it  Use the Close statement filMyFile.Close()  Only close files if they’re open  Once you close it, you can’t close it again until it’s open

 You can add text to an existing file  Open it with the AppendText command  Then just write to it as you would any other text file  When finished, just close it as you would any other text file

Dim filTestFile As StreamWriter filTestFile = File.CreateText(“mytest.txt”) filTestFile.WriteLine(“This is the first line of text!”) filTestFile.WriteLine(“Here is the second!”) filTestFile.WriteLine(“”) filTestFile.WriteLine(“The previous line was blank!!”) filTestFile.Close() filTestFile = File.AppendText(“mytest.txt”) filTestFile.WriteLine(“”) filTestFile.WriteLine(“Oops. We forgot to add this line the first time!”) filTestFile.Close()

 Object in Visual Basic used to read text files  Declare the variable with a Dim statement Dim filTestFile As StreamReader  To open the file, use the OpenText method: filTestFile = File.OpenText(filTestFile)  To read data from the file, use the ReadLine method Dim txtALine As String txtALine = filTestFile.ReadLine()  Data can ONLY be read in a forward direction

 Check to see if a file exists or not (FileExists) If File.Exists(strFileName) Then ‘ Do something here End If  You can line things up nicely with vbTab  It is a string, which adds one tab to the text lstMyListBox.items.Add(“Name: “ & vbTab & vbTab & “Bill Clinton”) lstMyListBox.items.Add(“Name: “ & vbTab & vbTab & “George Bush”)

 You can find if you’re at the end of the file with Peek Dim myFile As StreamReader Dim strMyStr As String myFile = File.OpenText(“grades.txt”) Do Until myFile.Peek = -1 strMyStr = myFile.ReadLine() lstMyListBox.Items.Add(strMyStr) Loop myFile.Close()