File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.

Slides:



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

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.
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.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
Computer Science 111 Fundamentals of Programming I Files.
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.
File I/O. Stream Objects cin – the standard input stream - an object of the istream class, cout – the standard output stream – an object of the ostream.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 1 CMT1000: Introduction to Programming Ed Currie Lecture 10: File Input.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Guide To UNIX Using Linux Third Edition
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
Understanding Input/Output (I/O) Classes Lesson 5.
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.
Files and Streams 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
File I/O 11_file_processing.ppt
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 C++ Syntax and Semantics, and the Program Development Process.
File I/O ifstreams and ofstreams Sections 11.1 &
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
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.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
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.
CS360 Windows Programming
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
File I/O Version 1.0. Topics I/O Streams File I/O Formatting Text Files Handling Stream Errors File Pointers.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 11.
BIM313 – Advanced Programming File Operations 1. Contents Structure of a File Reading/Writing Texts from/to Files File and Directory Operations 2.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15.
CSC 298 Streams and files.
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
C OMPUTER P ROGRAMMING 1 Input and Variables. I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
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.
File Operations. FILE PROCESSING For the purposes of the following discussion, reading means copying all or part of an existing file into memory Writing.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
File Output Writing data out to a file 1. Output to files  StreamWriter : An object in the System.IO namespace that lets you print output to a destination.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
Topic: File Input/Output (I/O)
Basic Text File Input/Output
Using the Console.
Chapter 7 Text Input/Output Objectives
I/O Basics.
Files and Streams.
Files and Streams Lect3 CT1411.
File Input/Output (I/O)
Topics Introduction to File Input and Output
Fundamentals of Programming I Files
Files & Streams.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Input and Output.
Files and Streams Lect10 GC201 12/1/2015.
Fundaments of Game Design
Topics Introduction to File Input and Output
BO65: PROGRAMMING WRITING TO TEXT FILES.
Little Man Computer There’s a little man in the mailroom that follows each instruction to the letter but he can only follow one instruction at a time.
Files and Streams.
Topics Introduction to File Input and Output
Presentation transcript:

File I/O Static void Main( ) {... } data

Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers

Objectives After completing this topic, students should be able to: Write programs that correctly read text data from a file, handling file errors and end of file conditions appropriately. Write programs that correctly format and write text data to a file.

Stream Objects the standard input stream the standard output stream These streams are automatically created for you when your program executes. We use methods in the Console class to use these streams. WriteLine(“Hello”);//output stream ReadLine();//input stream We have been using stream objects all semester.

To do file I/O, we will use a new set of file I/O stream classes StreamReader - objects of this class provide text file input StreamWriter – objects of this class provide text file output

File I/O When a program takes input from a file, we say that it reads from the file. When a program puts data into a file, we say that it writes to the file. To read or write to a file, we create a stream object, and connect it to the file.

seek time is the time required for the read head to move to the track containing the data to be read. The disk drive is a mechanical device so it operates at a much slower speed than the computer’s processor

rotational delay or latency, is the time required for the sector to move under the read head.

Because it is a mechanical device, the disk drive is also subject to many more errors than the computer’s processor.

Text Files Data in a file can either be text or binary. Everything in a text file appears as readable characters. You can look at the file with a text editor, such as notepad. Text files are sometimes referred to as Formatted files. This semester we will only deal with text files.

The StreamReader class We use objects of the StreamReader class to read data from a file. To use the StreamReader class we have to write using System.IO; at the beginning of our program.

Creating A StreamReader object StreamReader myData = new StreamReader(“theFile.txt”); “theFile.txt” is the path to the file in the current directory “C:\\theFile.txt” will access the file at the root of the C: partition or use the fully qualified path “partition:\\directory\\...\\filename.txt”

StreamReader Methods The StreamReader class contains the ReadLine( ) method, which works exactly like its counterpart in the Console class.

StreamReader fileData = new StreamReader(“thefile.txt”); string s = “”; int num1 = 0; double num2 = 0.0; s = fileData.ReadLine( ); num1 = int.Parse(fileData.ReadLine( ) ); num2 = double.Parse(fileData.ReadLine( ) ); Example Code

The StreamWriter class We use objects of the StreamWriter class to write data to a file. To use the StreamWriter class we also have to write using System.IO; at the beginning of our program.

Creating A StreamWriter object StreamWriter myData = new StreamWriter(“theFile.txt”);

StreamWriter Methods The StreamWriter class contains the Write( ) and WriteLine( ) methods, which works exactly like their counterparts in the Console class.

StreamWriter fileData = new StreamWriter(“thefile.txt”); string name = “John”; int age = 32; fileData.WriteLine(“My name is {0}”, name); fileData.WriteLine(“I am {0} years old.”, age); Example Code

Stream variables In the statement StreamReader theData = new StreamReader(“myFile.txt”); The variable theData is a reference variable. It “points” to the stream object that this statement created.

Connecting a Stream to a File StreamReader inputStream = new StreamReader(“theData.txt”); Widget 123V theData.txt inputStream program stream object

Paths StreamReader inputStream = new StreamReader(“theData.txt”); if no path is specified, the file is assumed to be in the same directory as the executable file.

Paths StreamReader inputStream = new StreamReader(“c:\\theData.txt”); When you code a \ in a pathname, you must write \\. Why? StreamReader inputStream = new StreamReader(“c:/theData.txt”); You can also use either of the following techniques To show the pathname: StreamReader inputStream = new

Opening an Output file If the named file does not exist, the file is created. If the named file already exists, it is opened, and the contents of the file are discarded, by default.

Formatting the Output Most of the time, when we write data to a file, it is with the idea in mind that the data will be read in from this or some other program. It is up to the programmer to format the data in the output file, so that it can later be read in a meaningful way.

Example int a = 5; int b = 15; int c = 239; StreamWriter output = new StreamWriter(“data.txt”); output.Write(“{0}{1}{2}”, a, b, c); What happens when you try to read this file?

Writing a loop that reads until end of file When we read data from a file, most often we have no idea how much data there is in the file. So, we need a scheme that let’s us continue reading and processing data until we reach the end of the file.

If we have reached the end of the file, the ReadLine( ) method returns a null. This condition can be tested with a statement like: if (inputString != null) …

Activity Diagram Read a line of data into a string string is null done Process the data

string theData; StreamReader myFile = new StreamReader(“data.txt”); do { Start the read loop

string theData; StreamReader myFile = new StreamReader(“data.txt”); do { theData = myFile.ReadLine( ); //Read some data

string theData; StreamReader myFile = new StreamReader(“data.txt”); do { theData = myFile.ReadLine( ); if (theData != null) { //Is the input null?

string theData; StreamReader myFile = new StreamReader(“data.txt”); do { theData = myFile.ReadLine( ); if (theData != null) { // process the data just read }

string theData; StreamReader myFile = new StreamReader(“data.txt”); do { theData = myFile.ReadLine( ); if (theData != null) { // process the data just read } } while (theData != null); //Loop until the input is null

Binary I/O We will not do any binary I/O programs in this course. Data is written to the output device exactly as it Is stored in memory via a byte by byte copy. Binary I/O is done using the BinaryWriter class.

Practice Write a program for the diving competition at the Olympic games. In the diving competition, each dive is scored by a panel of judges. The scores are totaled and the highest and lowest scores are then subtracted from the total. The average is computed for the remaining scores. This is the score awarded for the dive. Given: You have a file of judge’s scores in your documents folder. The name of the file is given by the user. The file is a text file. You do not know how many scores are in the file, but it is less than 9. Each score is a real number between 0 and 10, e.g. 8.85