Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
File streams Chapter , ,
1 Lecture 7  Fundamental data types in C  Data type conversion:  Automatic  Casting  Character processing  getchar()  putchar()  Macros on ctype.h.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
Overview creating your own functions calling your own functions.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CS Nov 2006 C-strings.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
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.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
1 Chapter 10 Characters, Strings, and the string class.
CHAPTER 8 CHARACTER AND STRINGS
1 Chapter 9 Scope, Lifetime, and More on Functions.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Functions Why we use functions C library functions Creating our own functions.
CSE 232: C++ Input/Output Manipulation Built-In and Simple User Defined Types in C++ int, long, short, char (signed, integer division) –unsigned versions.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
Characters, Strings, And The string Class Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
Module 4: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 4: I/O In this module we will cover Keyboard/screen input and output.
Overview of c++ Objectives 1. Understanding the use of the following elements in a c++ program variables constants assignment input output 2. Writing a.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
CSC Programming for Science Lecture 8: Character Functions.
Chapter Characters, Strings, and the string class 10.
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
CTYPE.H Introduction  The ctype header is used for testing and converting characters.  A control character refers to a character that.
Input/Output in C++ C++ iostream.h instead of stdio.h Why change? –Input/output routines in iostream can be extended to new types declared by the user.
1 Chapter 4 Program Input and the Software Design Process.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 6 I/O Streams as an Introduction to Objects and Classes.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
13/15/2016CS150 Introduction to Computer Science 1 Summary  Assignment due on Wednesday, October 29,  Tutor will be in the lab on Tuesday evening,
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
C Characters and Strings
Characters, C-Strings, and More About the string Class
Chapter 8 - Characters and Strings
Standard Version of Starting Out with C++, 4th Edition
Decision Making.
10.1 Character Testing.
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
CS150 Introduction to Computer Science 1
Standard Version of Starting Out with C++, 4th Edition
Introduction to C EECS May 2019.
C Characters and Strings
Presentation transcript:

Data Types & I/O Streams Objectives Data Types Understand that integers form the underlying foundation for many data types. Introduce a few frequently used char functions Introduce enum types as a means of defining your own types! I/O Streams Using streams for I/O with files. Some functions for processing data files.

Integers n bits -> can represent 2 n values Example : in 5 bits one can represent 2 5 = 32 values TYPEbitssmallestlargest int short unsigned int long int unsigned long int char is formed as an 8 bit integer and can be used as as an integer (-127 to 127) or unsigned char (0 to 255) enum is also basically an int as we will see.

Casting types When the type isn't exactly what is needed, one can sometimes employ a cast to convert it to the proper type. Example: // will output // value of 5 / 2 is 2 cout << "value of 5 / 2 is " << (5 / 2) << endl; // will output // value of 5 / (float)2 is 2.5 cout << "value of 5 / (float) 2 is " << (5 / (float) 2) << endl; Others : int i; i = 'A'; // automatic conversion to integer 65 i = (int)'A'; // this is the same, but with cast i = int('A'); // uses the function form to cast // in all cases i gets the value 65

enum Lets you define a set of values.. A TYPE. Examples: enum day {sunday, monday, tuesday, wednesday,thursday, friday, saturday}; day appointment; // variable appointment // type is day appointment = monday; // VALUE assigned is monday What are they really?.... integer constants sunday = 0, monday = 1, tuesday=2, etc. What can you do with them? document anything you can do with an integer; What CAN'T you do with them? I/O (unless you only enter/expect integers) see Chapter 3, p. 165

Useful char functions In order to process input data, it is sometimes useful to be able to ask questions about what type of value it is. See p PrototypeDescription int isdigit (int c)Returns true if c is a digit, and false otherwise int isalpha (int c)Returns true if c is a letter, and false otherwise int isalnum (int c)Returns true if c is a digit or a letter, and false otherwise int isxdigit (int c)Returns true if c is a hexidecimal character, and false otherwise. int islower (int c)Returns true if c is a lowercase letter, and false otherwise int isupper (int c)Returns true if c is an uppercase, and false otherwise int tolower (int c)If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, toupper returns the argument unchanged. int toupper (int c)If c is an lowercase letter, toupper returns c as an uppercase letter. Otherwise, tolower returns the argument unchanged....

Useful char functions (cont.) cout A cout a.... isdigit('A')... // returns false.... isalpha('A')... // returns true.... islower('A')... // returns false.... isupper('A')... // returns true You need not be too concerned about these now. The need for these types of functions will come much later. You should simply be aware of their existence.

I/O Streams Remember REDIRECTION? It lets you read from a file! drake> a.out <inputfile We have substituted the disk file inputfile for the standard input file (keyboard) What if we wanted to read from more than one file at the same time? student registration file -> prepare bill -> tuition bill student info file -> Its really not much different than using cin and cout. Why call it a STREAM? All devices can be viewed simply as a stream of chars (like a river is a stream of water) whether it is an input or output stream.

An example of an I/O stream Copies an input file to an output file (cp!) like cp datafile outfile in unix

More on the stream functions See Chapter 11, section , pp open, get, put, eof, fail, close (these are enough for now) You can have as many file streams as variables (close) put and get are used for reading files a char at a time THEY DO NOT SKIP SPACES etc! You can use cin type input ( >> ) but it skips white space. Remember how cin does the same? To read three integers from each file above: Use >> or << for numbers and put get for char

cerr What if you wanted to output an error but the person was redirecting the output to a file? drake> a.out >outfile YOU WOULDN'T SEE THE ERROR! The cerr stream is a special kind of output which would NOT be redirected to the standard output device. For example: ins.open("datafile"); if (ins.fail()) {cerr << "UNABLE TO OPEN DATAFILE.TERMINATING\n"; return 1; } Whether the user redirects or not, this would print on the screen if the file did not exist. USE cerr TO OUTPUT ERROR MESSAGES!

Output Manipulators cout and any ofstream

Input Manipulators cin and any ifstream