Sequential Input and Output using Text Files

Slides:



Advertisements
Similar presentations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Advertisements

Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Chapter 11 Data Files Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Files & Streams. Files Introduction Files are used for long-term retention of large amounts of data, even after the program that created the data terminates.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
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.
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.
File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of.
Putting Applets into Web Pages.  Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ The html.
File I/O 11_file_processing.ppt
1 14/10/58Dr.Mud-Armeen Munlin 1 Files and Streams ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Lecture Set 12 Sequential Files and Structures Part A – Dialog Boxes, Filters, Directories.
Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Object Oriented Software Development 10. Persistent Storage.
File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams -- Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
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.
Error Trapping Exception Catching 1. Syntax & Compile-time  VB – Editor/Compiler Logic and Design  You Runtime  You Types of Errors: 2 When your program.
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.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 14: Sequential Access Files
INF230 Basics in C# Programming
Microsoft Visual Basic 2005: Reloaded Second Edition
EECS 183 Discussion #9: It’s time to “Git” Python! November 22nd, 2016
C# Programming: From Problem Analysis to Program Design
CSCI 3327 Visual Basic Chapter 11: Files and Streams
Files.
Building Java Programs
Managing results files
Using Multiple Forms.
How to work with files and data streams
Files and Streams Lect3 CT1411.
File Input/Output (I/O)
Files I/O, Streams, I/O Redirection, Reading with fscanf
Multi-form applications and dialogs
File Handling Programming Guides.
Topics Introduction to File Input and Output
CSS 161: Fundamentals of Computing
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Files & Streams.
The University of Texas – Pan American
Fundamentals of Data Structures
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Video list editor BIS1523 – Lecture 24.
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams -- Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides.
Text / Serial / Sequential Files
Files and Streams Lect10 GC201 12/1/2015.
Tonga Institute of Higher Education
Fundaments of Game Design
CIS16 Application Development and Programming using Visual Basic.net
Topics Introduction to File Input and Output
BO65: PROGRAMMING WRITING TO TEXT FILES.
How to work with files and data streams
Reading Data From and Writing Data To Text Files
Topics Introduction to File Input and Output
Chapter 11 Saving Data and Objects In Files
4.4 – List vs Array Exercise 4.1: Array Variables
Presentation transcript:

Sequential Input and Output using Text Files Text File I/O

Open and Save File Dialogs Using pre-built standard dialogs to select files to be opened and saved

Caveat These dialogs were designed to use in Windows GUI applications rather than console apps To make them work with console applications: You must add a reference to System.Windows.Forms (see next slide) Add a using statement for the System.Windows.Forms namespace Add the compiler attribute [STAThread] to the Main method in your console application

Adding a Reference

OpenFileDialog Multiform Applications November 10, 2018

OpenFileDialog Example Create dialog instance Caption for the dialog Filter: limits which files show in the dialog If the user clicked OK in the dialog, process the selected file When dialog first opens, initial directory is folder where app’s .exe is found Filter contains sequences of “description|*.ext” where “description” is a phrase that will be shown describing what is being shown and “*.ext” specifies which file extensions are to be shown Multiform Applications November 10, 2018

OpenFileDialog Example Caption Initial Directory Filter Only .txt files shown Dropdown filter ComboBox Expanded Multiform Applications November 10, 2018

SaveFileDialog Multiform Applications November 10, 2018

SaveFileDialog Example Create instance Set initial directory, caption, and filters Display dialog, and, if not cancelled by user, process the selected file Multiform Applications November 10, 2018

Designate a Relative Path to file

Folder Structure

SaveFileDialog Example Caption Initial Directory Only *.txt files shown Filter Multiform Applications November 10, 2018

Simple Text File I/O Simple reading and writing from/to a Text File Multiform Applications November 10, 2018

Text File I/O The StreamReader and StreamWriter classes are provided by .NET for simple text file I/O They must be opened before the first I/O operation and closed after the last I/O operation Failure to close a file may result in loss of data so it is important to close a file after its last I/O operation The OpenFileDialog and SaveFileDialog may be used to help to associate actual files with StreamReader and StreamWriter objects in a C# program Multiform Applications November 10, 2018

Opening the StreamReader StreamReader constructor takes a string FileName This shows opening a StreamReader object associated with an existing file selected with an OpenFileDialog. Note: this operation could fail for a variety of reasons, so it should be in a try/catch. Multiform Applications November 10, 2018

Closing the StreamReader Close the StreamReader – if we successfully opened it Note that this code is in the Finally block since we want to close the file even if an exception occurred in reading data from the file Multiform Applications November 10, 2018

Reading From a StreamReader Read one line of data from the StreamReader The rest of the while loop processes the data but is unrelated to reading from the StreamReader This file is comma-delimited, but that does not always apply in other cases While there is more data … Multiform Applications November 10, 2018

Putting it all together … Array of String Multiform Applications November 10, 2018

Opening a StreamWriter for Output File is an output only file File Mode – see next slide File name from the dialog In a try block FileMode.Create says if the file does not already exist, create it. If the file does exist, it will be erased and replaced by the new file. Multiform Applications November 10, 2018

FileMode Member name Description CreateNew Create Open OpenOrCreate Create a new file. This requires Write access. If the file already exists, an IOException is thrown. Create Create a new file. If the file already exists, it will be overwritten. Requires Write access. Open Open an existing file. A FileNotFoundException is thrown if the file does not exist. OpenOrCreate Specifies that the operating system should open a file if it exists; otherwise, a new file should be created. Truncate Specifies that the operating system should open an existing file. Once opened, the file should be truncated so that its size is zero bytes. Attempts to read from a file opened with Truncate cause an exception. Append Opens the file if it exists and seeks to the end of the file, or creates a new file.

FileAccess Member name Description Read Write ReadWrite Read access to the file. Data can be read from the file. Combine with Write for read/write access. Write Write access to the file. Data can be written to the file. Combine with Read for read/write access. ReadWrite Read and write access to the file. Data can be written to and read from the file.

Writing data to a StreamWriter Write one line of data to the output file; note that fields in the data are “comma-delimited” as a result of this WriteLine Multiform Applications November 10, 2018

Closing the File after Writing If the file was opened successfully, close it even if an I/O exception was thrown during the output operations Multiform Applications November 10, 2018

Putting it all together for output Multiform Applications November 10, 2018

Reading an Entire Text File Verify that the file with this name exists Attempt to open the file Read the entire file into a single string variable The above code should be in a try block since many exceptions are possible