01204111 Computer and Programming File I/O File Input/Output Author: Chaiporn Jaikaeo, Jittat Fakcharoenphol Edited by Supaporn Erjongmanee Lecture 13.

Slides:



Advertisements
Similar presentations
Chapter 2: Basic Elements of C++
Advertisements

C++ Introduction.
Introduction to Eclipse. Start Eclipse Click and then click Eclipse from the menu: Or open a shell and type eclipse after the prompt.
Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Computer and Programming
Method parameter passing, file input/output Computer and Programming.
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.
INTRODUCTION T.Najah Al_Subaie Kingdom of Saudi Arabia Prince Norah bint Abdul Rahman University College of Computer Since and Information System CS240.
Chapter 9: Sequential Access Files and Printing
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
Chapter 8 Arrays and Strings
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Understanding Input/Output (I/O) Classes Lesson 5.
Input/Output CE 311 K - Introduction to Computer Methods Daene C. McKinney.
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.
Computer Programming 1.  Editor Console Application Notepad Notepad++ Edit plus etc.  Compiler & Interpreter Microsoft.NET Framework  Microsoft visual.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
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.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
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 Input and Output (I/O) Engineering 1D04, Teaching Session 7.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
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.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
BIM313 – Advanced Programming File Operations 1. Contents Structure of a File Reading/Writing Texts from/to Files File and Directory Operations 2.
Text Files and String Processing
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Building Your Own Class. We will build a simple instance class for an inventory control application and a main program to test it. We need an Item class.
CSC 298 Streams and files.
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
Compunet Corporation1 Programming with Visual Basic.NET Input and Output Files Lecture # 6 Tariq Ibn Aziz.
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
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.
Strings in C++/CLI us/library/system.string.aspxhttp://msdn.microsoft.com/en- us/library/system.string.aspx public: static.
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.
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.
1 Statements © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License.
Searching, Modifying, and Encoding Text. Parts: 1) Forming Regular Expressions 2) Encoding and Decoding.
Visual BASIC Programming For CCS 301 course Dr. Ahmad ABDELHAY.
Files and Streams Lec Introduction  Files are used for long-term retention of large amounts of data, even after the program that created the.
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.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Lecture 18 File I/O Richard Gesick. File Input / Output Consider that a program may want to make its data persistent (or not rely upon the user for input)
C# — Console Application
Lecture 2 Data Types Richard Gesick.
Building Your Own Class
USING ECLIPSE TO CREATE HELLO WORLD
Chapter 4 – Control Structures Part 1
Files and Streams.
Files and Streams Lect3 CT1411.
File Input/Output (I/O)
Dosyalar.
Advanced Programming Lecture 02: Introduction to C# Apps
Files & Streams.
Fundamentals of Data Structures
Chapter 3.5 Input and Output
Introduction to C++ Programming
Files and Streams Lect10 GC201 12/1/2015.
Lecture 16 File I/O Richard Gesick.
Fundaments of Game Design
CIS16 Application Development and Programming using Visual Basic.net
BO65: PROGRAMMING WRITING TO TEXT FILES.
Files and Streams.
IS 135 Business Programming
Presentation transcript:

Computer and Programming File I/O File Input/Output Author: Chaiporn Jaikaeo, Jittat Fakcharoenphol Edited by Supaporn Erjongmanee Lecture September 2011

Computer and Programming File I/O Outline Introduction Writing to a file Reading from a file File types 2

Computer and Programming File I/O Introduction: File Input/Output Monitor output of a program disappears when the program terminates. We can store output of the program by writing into a file. The stored data can be used later. Thus, we need to read input or write output to a file 3

Computer and Programming File I/O Outline Introduction Writing to a file Reading from a file File types 4

Computer and Programming File I/O Write to a file using System; using System.IO; namespace fileio { class Program { public static void Main() { StreamWriter sw = new StreamWriter("test.txt"); sw.WriteLine("Hello world"); sw.Close(); } } } First, add using System.IO; on the top. This program opens a file named "test.txt” and writes "Hello world" to a file. 5

Computer and Programming File I/O Write to a file (cont.) Statements to write to a file contain 3 steps. 1.Open the data stream of output 2.Write data to output file 3.Close the data stream of output StreamWriter sw = new StreamWriter("test.txt"); sw.WriteLine("Hello world"); sw.Close(); StreamWriter sw = new StreamWriter("test.txt"); sw.WriteLine("Hello world"); sw.Close(); 6

Computer and Programming File I/O Write to a file: Step 1, Open the data stream of output – To create a StreamWriter object, we use operator new, and pass the file name. – The first line creates new StreamWriter object with the variable name sw that associates with the output file "text.txt” StreamWriter sw = new StreamWriter("test.txt"); sw.WriteLine("Hello world"); sw.Close(); StreamWriter sw = new StreamWriter("test.txt"); sw.WriteLine("Hello world"); sw.Close(); StreamWriter var_name = new StreamWriter(filename); 7 Syntax

Computer and Programming File I/O Write to a file: Step 2, Write data to output file – After creating StreamWriter object, we use Write or WriteLine to write to a file. – Almost similar to use Console.Write or Console.WriteLine Use StreamWriter variable name (e.g., sw) in place of Console. The result will be written to the file, instead of monitor. – The second line writes “Hello world” to file “test.txt” StreamWriter sw = new StreamWriter("test.txt"); sw.WriteLine("Hello world"); sw.Close(); StreamWriter sw = new StreamWriter("test.txt"); sw.WriteLine("Hello world"); sw.Close(); 8 var_name.Write(text); Syntax var_name.WriteLine(text);

Computer and Programming File I/O Write to a file: Step 3, Close the data stream of output – After done writing, close the StreamWriter object using Close(). – Use StreamWriter variable name (e.g., sw) with Close. StreamWriter sw = new StreamWriter("test.txt"); sw.WriteLine("Hello world"); sw.Close(); StreamWriter sw = new StreamWriter("test.txt"); sw.WriteLine("Hello world"); sw.Close(); 9 var_name.Close(); Syntax

Computer and Programming File I/O Outline Introduction Writing to a file Reading from a file File types 10

Computer and Programming File I/O Read from a file Instead of StreamWriter, we use StreamReader to create input data stream. To read from a file, we also need 3 steps 1.Use new to create StreamReader object that associates with specified input filename 2.Use StreamReader variable name (e.g., sr) in place of Console, to read from the associated file. 3.Close StreamReader object using System; using System.IO; namespace fileio { class Program { public static void Main(string[] args) { StreamReader sr = new StreamReader(”input.txt"); string p = sr.ReadLine(); string q = sr.ReadLine(); Console.WriteLine(“{0} {1}”, p, q); Console.ReadLine(); sr.Close(); } } } using System; using System.IO; namespace fileio { class Program { public static void Main(string[] args) { StreamReader sr = new StreamReader(”input.txt"); string p = sr.ReadLine(); string q = sr.ReadLine(); Console.WriteLine(“{0} {1}”, p, q); Console.ReadLine(); sr.Close(); } } } 11

Computer and Programming File I/O Example : Read from a file Here, StreamReader variable name (e.g., reader) that associates with the file “test.txt” using System; using System.IO; namespace fileio { class Program { public static void Main(string[] args) { StreamReader reader = new StreamReader("test.txt"); int a = int.Parse(reader.ReadLine()); int b = int.Parse(reader.ReadLine()); Console.WriteLine(a + b); Console.ReadLine(); reader.Close(); } } } using System; using System.IO; namespace fileio { class Program { public static void Main(string[] args) { StreamReader reader = new StreamReader("test.txt"); int a = int.Parse(reader.ReadLine()); int b = int.Parse(reader.ReadLine()); Console.WriteLine(a + b); Console.ReadLine(); reader.Close(); } } } Input data in file "test.txt" 8234 Monitor a 1234 b

Computer and Programming File I/O Example 2 : Read from a file This program reads input file as in the following example. using System; using System.IO; class Program{ public static void Main(string[] args) { StreamReader reader = new StreamReader("input.txt"); int n = int.Parse(reader.ReadLine()); int i = 1; int total = 0; while(i <= n) { int x = int.Parse(reader.ReadLine()); total += x; i++; } Console.WriteLine(total); reader.Close(); } } using System; using System.IO; class Program{ public static void Main(string[] args) { StreamReader reader = new StreamReader("input.txt"); int n = int.Parse(reader.ReadLine()); int i = 1; int total = 0; while(i <= n) { int x = int.Parse(reader.ReadLine()); total += x; i++; } Console.WriteLine(total); reader.Close(); } } Input file: input.txt 10 Monitor 13 This program reads 3 numbers (as the first number in “input.txt”) and computes the sum of these 3 numbers.

Computer and Programming File I/O Where is input/output file? In SharpDev, use the Project browser (the left panel) to locate the files. Click "Show all files" to see this file. Go to bin -> Debug to find your file. (e.g., test.txt) Double-click at the file name to see the content. show all files NOTE: After running the program, you will see a new console pops up and quickly disappears. This is the correct behavior. In folder bin/Debug

Computer and Programming File I/O Outline Introduction Writing to a file Reading from a file File types 15

Computer and Programming File I/O File Types Text file – Stores data as a sequence of readable lines – E.g., files with extensions.txt,.csv Binary file – Stores data that are encoded in binary form – E.g., files with extensions.exe 16

Computer and Programming File I/O CSV Files CSV CSV = Comma-Separated Values – A special type of text files used for storing tabular data – Each row represents a table entry – Each column is separated by a comma – Simple for reading and writing – Can be opened by many applications E.g., Microsoft Excel

Computer and Programming File I/O Example: CSV FileIDNameGPA 308John Tom Paula2.98 Data 308,John, ,Tom, ,Paula,2.98 CSV File “data.csv”

Computer and Programming File I/O Any question?