Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.

3 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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

4 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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

5 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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;

6 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.

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

8 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.

9 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.

10 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.

11 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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);

12 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.

13 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.

14 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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.

15 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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);

16 نظام المحاضرات الالكترونينظام المحاضرات الالكتروني 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"; }


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

Similar presentations


Ads by Google