CS360 Windows Programming

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.
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Getting Started with C# 1 SWE 344 Internet Protocols & Client Server Programming.
Tirgul 1 Today’s subject - Java reminders and additions: –Inner classes –Packages –I/O streams –Command Line Arguments –Primitive and Reference Data Types.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Understanding Input/Output (I/O) Classes Lesson 5.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
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.
CSC – Java Programming II Lecture 9 January 30, 2002.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
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.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
PRIMITIVE DATA TYPES -Integer -Floating Point -Decimal -Boolean -Character STRINGS -Character Array -Class -String Length -Static -Limited Dynamic -Dynamic.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
CS_OOP Lecture #2: Computer Hardware/Software; Variables, C# Data Types, and IO.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
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.
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.
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.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
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.
BIM313 – Advanced Programming File Operations 1. Contents Structure of a File Reading/Writing Texts from/to Files File and Directory Operations 2.
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
Text Files and String Processing
1 9/22/05CS360 Windows Programming Arrays, Collections, Hash Tables, Strings.
Input and Output 23: Input and Output
CSC 298 Streams and files.
CSE 1201 Object Oriented Programming ArrayList 1.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Characters and Strings
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.
Comp 335 – File Structures Hexadecimal Dumps Interpreting File Contents.
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.
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.
Files and Streams Lec Introduction  Files are used for long-term retention of large amounts of data, even after the program that created the.
Chapter 6: Creating Windows–based Applications 1 Microsoft® Visual C# 2008.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Module 6: Building .NET–based Applications with C#
using System; namespace Demo01 { class Program
Input and Output 23: Input and Output
C# Programming: From Problem Analysis to Program Design
EPSII 59:006 Spring 2004.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
How to work with files and data streams
Files and Streams.
Files and Streams Lect3 CT1411.
Sequential Input and Output using Text Files
File Input/Output (I/O)
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Lecture 13 Input/Output Files.
Files and Streams Lect10 GC201 12/1/2015.
Topics Introduction to File Input and Output
How to work with files and data streams
Files and Streams.
Interfaces, Enumerations, Boxing, and Unboxing
Presentation transcript:

CS360 Windows Programming File I/O, Arrays 9/20/05 CS360 Windows Programming

CS360 Windows Programming Last Time We began looking at GUI Programming Completed talking about exception handling 9/20/05 CS360 Windows Programming

CS360 Windows Programming File Input/Output .NET Framework supports file I/O in the System.IO namespace What is the difference between files and streams File: collection of data stored on a disk with a name and directory path Stream: once a file is opened for reading or writing it becomes a stream. A stream is something on which you can perform read and write operations 9/20/05 CS360 Windows Programming

CS360 Windows Programming Text File Example using System; using System.IO; public class TextToFile { private const string FILE_NAME = "MyFile.txt"; public static void Main(String[] args) if (File.Exists(FILE_NAME)) Console.WriteLine("{0} already exists.", FILE_NAME); return; } StreamWriter sr = File.CreateText(FILE_NAME); sr.WriteLine ("This is my file."); sr.WriteLine ("I can write ints {0} or floats {1}, and so on.", 1, 4.2); sr.Close(); 9/20/05 CS360 Windows Programming

CS360 Windows Programming Another Example using System; using System.IO; public class StreamsIOApp { public static void Main(string[] args) StreamWriter s = new StreamWriter("Foo.txt"); s.Write("some text or other"); s.Close(); StreamReader r = new StreamReader("Foo.txt"); for( string line = r.ReadLine(); line != null; line = r.ReadLine() ) Console.WriteLine( line ); r.Close(); } 9/20/05 CS360 Windows Programming

CS360 Windows Programming File I/O There are many ways to open and access files There is no best way The .NET Framework class library contains a complete list of file and stream classes See page 62 9/20/05 CS360 Windows Programming

CS360 Windows Programming ASCII vs. Binary Files What is the difference between ASCII and binary files? Why do we use binary files? 9/20/05 CS360 Windows Programming

CS360 Windows Programming Number Systems Hexadecimal, decimal, and binary How do we convert from binary to decimal? How do we convert from decimal to binary? How do we convert from binary to hex? Convert the decimal 512 to Hex Convert 512 first to binary Convert the binary to hex 9/20/05 CS360 Windows Programming

CS360 Windows Programming Text Files What are characters in a text file stored as? What is 62 stored as? http://www.lookuptables.com/ 9/20/05 CS360 Windows Programming

CS360 Windows Programming Example 32767 represented as a text file: Why? 00110011 00110010 00110111 00110110 00110111 00110011 00110010 00110111 00110110 00110111 51 50 55 54 55 3 2 7 6 7 Binary Decimal Character 9/20/05 CS360 Windows Programming

CS360 Windows Programming Example Decimal 32767 represented as a binary file: What is that in Hex? 01111111 11111111 9/20/05 CS360 Windows Programming

CS360 Windows Programming Binary and Text Files The difference between binary and text files is the way they write data to the files In binary files, the binary representation of values is written to the file The integer ‘4929067’ which takes 4 bytes in memory will also take 4 bytes in the file In case of text file, each value is written as a series of characters (ASCII or Unicode) The integer ‘4929067’ will be written as text and will take 7 bytes in ASCII encoding and 14 (7 x 2) bytes in Unicode encoding Binary files are more efficient for reading and writing of data for machines while text files are more human readable. 9/20/05 CS360 Windows Programming

Example to Write a Binary File Stream s = new FileStream("Foo.txt", FileMode.Create); StreamWriter w = new StreamWriter(s); w.Write("Hello World "); w.Write(32767); w.Write(' '); w.Write(45.67); w.Close(); s.Close(); Stream t = new FileStream("Bar.dat", FileMode.Create); BinaryWriter b = new BinaryWriter(t); b.Write("Hello World "); b.Write(512); b.Write(' '); b.Write(45.67); b.Close(); t.Close(); 9/20/05 CS360 Windows Programming

CS360 Windows Programming Hex Dump Write a program that will read a file and output the hex values of that file 9/20/05 CS360 Windows Programming

CS360 Windows Programming Arrays Single dimension int[] myInts = new int[20]; . . . Console.WriteLine(myInts[i]); Multidimension string[,] myStrings = new string[5,6]; double[,,] myDoubles = new double[3,8,5]; Console.WriteLine(myDoubles[i,j,k]); Jagged Point[][] myPolygons = new Point[3][]; myPolygons[0] = new Point[10]; myPolygons[1] = new Point[20]; myPolygons[2] = new Point[30]; for (int x = 0; x < myPolygons[1].Length; x++) Console.WriteLine(myPolygons[1][x]); 9/20/05 CS360 Windows Programming

CS360 Windows Programming Point[][] myPolygons = new Point[3][]; myPolygons[0] = new Point[10]; myPolygons[1] = new Point[20]; myPolygons[2] = new Point[30]; for (int x = 0; x < myPolygons[1].Length; x++) Console.WriteLine(myPolygons[1][x]); foreach (Point p in myPolygons[1]) Console.WriteLine(p); Note: inside a foreach loop we have read only access to the array elements. 9/20/05 CS360 Windows Programming

CS360 Windows Programming int[] myInts; Creates a variable that can point to an array myInts = new int[100]; Creates an array of 100 ints. These ints are initialized to 0, and stored, unboxed, in a memory block on the managed heap. Control myControls; myControls = new Control[100]; Creates an array of Control references, initialized to null. Since Control is a reference type, creating the array creates references—the actual objects are not created. 9/20/05 CS360 Windows Programming

CS360 Windows Programming Array Initialization Single dimension int[] myInts = new int[3] {1, 2, 3}; int[] myInts = new int[] {1, 2, 3}; int[] myInts = {1, 2, 3}; 9/20/05 CS360 Windows Programming

Arrays are implicitly derived from System.Array int[] myInts = new int[3] {1, 2, 3}; double[,,] myDoubles = new double[3,8,5]; Properties myInts.Length is 3 myInts.Rank is 1 myDoubles.Length is 120 myDoubles.Rank is 3 myDoubles.GetLength(1) is 8 Static Methods Array.Sort(myInts); Array.Sort(keys, items); Array.Reverse(myInts); Array.Clear(myInts); int i = Array.IndexOf(myInts, 17); 9/20/05 CS360 Windows Programming

CS360 Windows Programming Summary Completed File I/O Arrays Covered p. 59-66 9/20/05 CS360 Windows Programming