Download presentation
Presentation is loading. Please wait.
1
Basic Input and Output Operations
The input and output functionality provided by the standard library involves using streams. A stream is an abstract representation of an input or output device that is a source or destination for data in your program. You can visualize a stream as a sequence of characters flowing between an external device and the main memory of your computer. In Figure 1 there is a simplified diagram showing the relationships between stream classes. Basic Input and Output tMyn
2
ios_base istream ios ostream iostream ifstream ofstream fstream
Records stream state, formatting controls istream Sequential & Random Input ios Tests and sets stream state ostream Sequential &, Random Output iostream Sequential & Random Input/Output ifstream File Input ofstream File Output fstream File Input/Output Figure 1. The main classes involved in stream input-output. Basic Input and Output tMyn
3
These are formatted input/output operations.
You can write data to an output stream and read data from an input stream. There are two modes for transferring data to and from streams: text mode and binary mode. In addition to the two modes of data transfer, there are two ways in which you can read from and write to a stream. Firstly, you can read and write various types of data using the extraction and insertion operators. These are formatted input/output operations. All the operations writing to cout and reading from cin in this course are formatted I/O operations in text mode. Basic Input and Output tMyn
4
These are unformatted input/output operations.
The second way of working with a stream is to read or write character-based data. A read or write operation can be for just a single character, a given number of characters, or a sequence of characters terminated by a delimiter of some kind, but the most significant point about this method is that you only read or write bytes. These are unformatted input/output operations. The physical reality of an output stream – in other words, where the data goes when you write to it – can be any device to which a sequence of bytes can be transferred. Basic Input and Output tMyn
5
A standard input stream, cin, is usually associated with the keyboard.
The standard library defines a standard output stream, cout, which is usually associated with the screen. A standard input stream, cin, is usually associated with the keyboard. When we are reading objects from cin using the extraction operator, >>, or writing objects to cout using the insertion operator, <<, we are using overloaded versions of the operator<<() and operator>>() functions for these objects. As far as the standard streams are concerned, everything is set up and ready to go. Basic Input and Output tMyn
6
The function operator>>() is implemented as a set of overloaded members of the istream class to support reading basic types of data from any input stream. There will be an overloaded version of this operator for each of the basic data types in C++. A whitespace character is always regarded as a delimiter between values, so you cannot read whitespace characters into your program using the operator>>() members of the istream class. Basic Input and Output tMyn
7
The operator<<() function is overloaded in the ostream class for formatted stream output of data values of the basic types. In addition, outputting of a single character or a null-terminated string is possible. Basic Input and Output tMyn
8
#include <iostream> using namespace System; using namespace std;
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; int main(array<System::String ^> ^args) { int age=49; char name[]="Timo"; cout<<"My name is " <<name<<" and I am " <<age<<" years old!"<<endl; return 0; } Basic Input and Output tMyn
9
#include <iostream> using namespace System; using namespace std;
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; int main(array<System::String ^> ^args) { int age; char name[15]; cout<<"Please, input your age and first name: "; cin>>age>>name; cout<<"Your name is "<<name <<" and you are "<<age <<" years old."<<endl; return 0; } Basic Input and Output tMyn
10
istream& ignore ( streamsize n = 1, int delim = EOF ); Extracts characters from input stream and discards them. Extraction ends when n characters have been discarded or when delim is found, whichever comes first. In this last case delim is also extracted. Basic Input and Output tMyn
11
In addition to the insertion and extraction operators for formatted stream input-output, there are member functions in the stream classes for transferring character-based data to or from a stream without any formatting of the data. The extraction operator treats whitespace characters as delimiters, but otherwise ignores them. In general, the unformatted stream input functions do not skip whitespace characters – they are read and stored just like any others. Basic Input and Output tMyn
12
For a start, there are many varieties of the member function get().
There is a wealth of unformatted input functions defined in the istream class, and they are inherited in the ifstream, iostream and fstream classes. For a start, there are many varieties of the member function get(). Basic Input and Output tMyn
13
#include <iostream> using namespace System; using namespace std;
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; int main(array<System::String ^> ^args) { int number; char cha1, cha2; cout<<"Please, input an integer: "; cin>>number; cin.ignore(); cout<<"Please, input one character: "; cin.get(cha1); Basic Input and Output tMyn
14
cout<<"Please, input another character: "; cin.get(cha2);
cout<<"The number was: "<<number<<"."<<endl; cout<<"The first character was: "<<cha1<<"."<<endl; cout<<"The second character was: "<<cha2<<"."<<endl; return 0; } istream& get (char& c ); Extracts a character from the stream and stores it in c. Basic Input and Output tMyn
15
Basic Input and Output tMyn
16
The other two get() functions read a sequence of characters as a null-terminated string:
istream& get (char* s, streamsize n); istream& get (char* s, streamsize n, char delim); These functions extract characters from the stream and stores them into successive locations in the array pointed by s. Characters are extracted until either (n - 1) characters have been extracted, the delimiter (parameter delim or '\n' if not specified) is found, or if the end of file or any error occurs in the input sequence. Basic Input and Output tMyn
17
If the delimiter is found it is not extracted from the input stream and remains as the next character to be extracted. Use the function getline() if you want this character to be extracted (and discarded). An ending null character is automatically appended at the end of the content stored in s. Basic Input and Output tMyn
18
#include <iostream> using namespace System; using namespace std;
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; int main(array<System::String ^> ^args) { int age; char wholeName[20], address[20]; cout<<"Please, input your age: "; cin>>age; cin.get(); cout<<"Please, input your whole name: "; cin.get(wholeName,20); cin.ignore(80,'\n'); Basic Input and Output tMyn
19
cout<<"Please, input your address: "; cin.get(address,20);
cin.ignore(80,'\n'); cout<<"Your age is: "<<age<<"."<<endl; cout<<"Your name is: "<<wholeName<<"."<<endl; cout<<"Your address is: "<<address<<"."<<endl; return 0; } istream& ignore ( streamsize n = 1, int delim = EOF ); Extracts characters from input stream and discards them. Extraction ends when n characters have been discarded or when delim is found, whichever comes first. In this last case delim is also extracted. Basic Input and Output tMyn
20
Basic Input and Output tMyn
21
Remember! If you add a delimiter to the function
istream& get (char* s, streamsize n, char delim), you must also have it in the input stream!!! Try it! Basic Input and Output tMyn
22
In addition, there are two getline() function members that are almost equivalent to the two get() functions that read a line of text: istream& getline (char* s, streamsize n); istream& getline (char* s, streamsize n, char delim); These functions extract characters from the stream and stores them into successive locations in the array pointed by s. Characters are extracted until either (n - 1) characters have been extracted, the delimiter (parameter delim or '\n' if not specified) is found, or if the end of file or any error occurs in the input sequence. Basic Input and Output tMyn
23
If the delimiter is found it is extracted but not stored
If the delimiter is found it is extracted but not stored. Use the function get() if you don't want this character to be extracted. An ending null character is automatically appended after the data stored in s. Basic Input and Output tMyn
24
#include <iostream> using namespace System; using namespace std;
#include "stdafx.h" #include <iostream> using namespace System; using namespace std; int main(array<System::String ^> ^args) { int age; char wholeName[30], address[30]; cout<<"Please, input your age: "; cin>>age; cin.get(); cout<<"Please, input your whole name: "; cin.getline(wholeName,30); Basic Input and Output tMyn
25
cout<<"Please, input your address: "; cin.getline(address,20);
cout<<"Your age is: "<<age<<"."<<endl; cout<<"Your name is: "<<wholeName<<"."<<endl; cout<<"Your address is: "<<address<<"."<<endl; return 0; } Basic Input and Output tMyn
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.