Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

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

3 01204111 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

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

5 01204111 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

6 01204111 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

7 01204111 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

8 01204111 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);

9 01204111 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

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

11 01204111 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

12 01204111 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 7000 12

13 01204111 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(); } } 3172531725 3172531725 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.

14 01204111 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

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

16 01204111 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

17 01204111 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

18 01204111 Computer and Programming File I/O Example: CSV FileIDNameGPA 308John3.21 546Tom2.35 121Paula2.98 Data 308,John,3.21 546,Tom,2.35 121,Paula,2.98 CSV File “data.csv”

19 01204111 Computer and Programming File I/O Any question?


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

Similar presentations


Ads by Google