نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.

Slides:



Advertisements
Similar presentations
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Advertisements

1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
1 Lecture 5: Input/Output (I) Introduction to Computer Science Spring 2006.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Chapter 3: Input/Output
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.
Unformatted and Formatted I/O Operations. 2 Unformatted Input/output is the most basic form of input/output. Unformatted I/O transfers the internal binary.
UNFORMATTED INPUT OUTPUT. Topics to be discussed……………….. overloaded operators >> and and
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.
1 Advanced Input and Output COSC1567 C++ Programming Lecture 9.
Chapter 8 Streams and Files Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
FILE HANDLING IN C++.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
Lecture Contents I/O Streams. –Input/output streams. –Unformatted vs formatted streams. –Stream manipulators. –Stream error state. –Stream tying. –Examples.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
1 Today’s Objectives  Announcements Turn in Homework 4 Quiz 4 will be on Wednesday, July 19 – It will have questions about inheritance, polymorphism,
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Chapter 15 Strings as Character Arrays
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 COMS 261 Computer Science I Title: Functions Date: October 24, 2005 Lecture Number: 22.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
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.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Streams and Files Problem Solving, Abstraction, and Design using.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
CS212: Object Oriented Analysis and Design
CS212: Object Oriented Analysis and Design
Introduction to C++ (Extensions to C)
Computing Fundamentals
Basic Input and Output Operations
I/O Basics.
Copyright © 2003 Pearson Education, Inc.
week 1 - Introduction Goals
Chapter 2 part #3 C++ Input / Output
Input and Output Chapter 3.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Introduction to C++ Programming
Advanced Input and Output
IO Overview CSCE 121 J. Michael Moore
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
Programming with Data Files
Today’s Lecture I/O Streams Tools for File I/O
Chapter 3: Input/Output
Managing console i/o operations
CSC 143 Stream I/O Classes and Files [A11-A15, A38-A50]
File I/O in C++ I.
File I/O.
CHAPTER 4 File Processing.
Chapter 2 part #3 C++ Input / Output
IO Overview CSCE 121 Strongly influenced by slides created by Bjarne Stroustrup and Jennifer Welch.
Input/Output Streams, Part 1
File I/O in C++ I.
Introduction to Programming
Presentation transcript:

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management Concept of streams. cin and cout objects. C++stream classes. Unformatted I/O.

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني C ++ Stream Stream is an interface supplied by the I/O system of C++ between the programmer and the actual device being accessed. It will work with devices like terminals, disks and tape drives. A stream is a sequence of bytes. It acts either as a source from which the input data can be obtained or as a destination to which the output data can be sent.

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني C ++ Stream Input Stream - The source stream that provides data to the program. Output Stream - The destination stream that receives output from the program. continue… Input device Output device Program Input Stream Output Stream Extraction from input stream Insertion into output stream

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني C ++ Stream The data in the input stream can come from keyboard or any other storage device. The data in the output stream can go to the screen or any other storage device. continue… Input device Output device Program Input Stream Output Stream Extraction from input stream Insertion into output stream

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني The cin/cout stream Pre-defined streams Automatically opened when a program begins its execution. Standard input/output stream Connected to the standard input/output devices Usually the keyboard/screen. Can redirect streams to other devices or files freopen("test.txt", "r", stdin); freopen("test.txt", "w", stdout); cout<<“Test it.”<<endl;

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني C ++ Stream Classes The C++ I/O system contains a hierarchy of classes that are used to define various streams to deal with both the console and disk files. These classes are called stream classes. These classes are declared in the header file iostream.

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني C ++ Stream Classes ios istreamostreamstreambuf iostream istream_withassigniostream_withassignostream_withassign continue…

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني C ++ Stream Classes ios istreamostreamstreambuf iostream istream_withassigniostream_withassignostream_withassign continue… Provides the basic support for formatted and unformatted I/O operations. Provides the facilities for formatted and unformatted input Provides the facilities for formatted output Provides the facilities for handling both input and output streams.

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Unformatted I/O Operations Overloaded Operators >> and << The objects cin and cout are used for input and output of data of various types. By overloading the operators >> and <<. >> operator is overloaded in the istream class. << operator is overloaded in the ostream class. This is used for input data through keyboard.

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Unformatted I/O Operations Overloaded Operators >> and << cin >> variable1 >> varibale2... >> variableN where variable1, variable2, …, variableN are valid C++ variable names. cout << item1 << item2 << … << itemN Where item1, item2, …,itemN may be variables or constants of an basic type.

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Unformatted I/O Operations put( ) and get( ) Functions get( ) and put( ) are member functions of istream and ostream classes. For single character input/output operations. There are two types of get( ) functions: get(char*)  Assigns the input character to its argument. get(void)  Returns the input character. char c; cin.get(c) c = cin.get( ); put( )  used to output a line of text, character by character. char c; cout.put(‘x’); cout.put(c);

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني put() and get() Example: 12 int main(){ char c; cin.get(c); while (c != '\n') { cout << c; cin.get(c); } //replace cin.get(c) with cin >> c and compare the result.

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني put() and get() Example: 13 cout.put(68)  ’D’ Example-code: cin.get(c); while(c != ‘ \n ’ ) { cout.put(c); cin.get(c); } The text is sent to the program as soon as we press the RETURN key. The program then reads and displays characters one by one. the process is terminated when the newline character is encountered.

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Unformatted I/O Operations getline( ) and write( ) Functions getline( ) function reads a whole line of text that ends with a newline character. cin.getline(line, size); Reading is terminated as soon as either the newline character ‘\n’ is encountered or size-1 characters are read. write( ) function displays an entire line of text. cout.write(line, size); write( ) also used to concatenate strings.

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Example of getline() 15 Example-code: int main() { int size = 20; char city[20]; cout << "Enter city name: \n"; cin >> city; cout << "City name: " << city <<"!\n\n"; cout << "Enter city name again: \n"; cin.getline(city,size); cout << "City name now: " << city <<“!\n\n"; cout << "Enter another city name: \n"; cin.getline(city,size); cout << "New city name: " << city << “!\n\n"; return 0; } cin.getline(city, size);

نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Example of write() Example-code: (program 10. int main(){ char * string1 = "C++ "; char * string2 = "Programing"; int m = strlen(string1); int n = strlen(string2); for (int i=1; i<n; i++){ cout.write(string2,i); cout << "\n"; } for(int i=n; i>0; i--){ cout.write(string2,i); cout << "\n"; } //concotenating strings cout.write(string1,m).write(string2,n); cout << "\n"; //crossing the boundary cout.write(string1,10); cout << "\n"; }