Fig: iostream class hierarchy
|
|
- Sharlene Booker
- 6 years ago
- Views:
Transcription
1 Unit 6: C++ IO Systems ================== Streams: Θ A stream is a logical device that either produces or consumes information. Θ A stream is linked to a physical device by the I/O system. Θ All streams behave in the same way even though the actual physical devices they are connected to may differ substantially. Θ Because all streams behave the same, the same I/O functions can operate on virtually any type of physical device. Fig: iostream class hierarchy Stream Meaning Default Device cin Standard input Keyboard cout Standard output Screen cerr Standard error Screen output clog Buffered Screen version of cerr Θ Streams cin, cout, and cerr correspond to C's stdin, stdout, and stderr. Formatted I/O =========== Θ C++ I/O system allows you to format I/O operations. Θ For example, you can set a field width, specify a number base, or determine how many digits after the decimal point will be displayed. Θ two related but conceptually different ways that you can format data. 1. Directly access members of the ios class. It means, you can set various format status flags defined inside the ios class or call various ios member functions. 2. you can use special functions called manipulators that can be included as part of an I/O expression. 1. Formatting Using the ios Members
2 Θ ios class declares a bitmask enumeration called fmtflags in which the following values are defined. Θ hex flag causes output to be displayed in hexadecimal. Θ left flag is set, output is left justified. Θ right is set, output is right justified. Θ oct flag causes output to be displayed in octal. Θ dec -To return output to decimal, set the dec flag. Θ showbase - causes the base(decimal,hex,oct) of numeric values to be shown. Θ uppercase If set, these characters are displayed in uppercase. Θ showpos - causes a leading plus sign to be displayed before positive values. Θ showpoint- causes a decimal point and trailing zeros to be displayed for all floating-point output Θ scientific flag- floating-point numeric values are displayed using scientific notation. Θ boolalpha is set, Booleans can be input or output using the keywords true and false. Θ basefield - oct, dec, and hex fields, they can be collectively referred to as basefield. Θ floatfield - scientific and fixed fields can be collectively referenced as floatfield. Setting the Format Flags =================== setf( ) function fmtflags setf(fmtflags flags) Θ This function returns the previous settings of the format flags and turns on those flags specified by flags. E.g: to turn on the showpos flag, you can use this statement: cout.setf(ios::showpos); Θ The following program displays the value 100 with the showpos and showpoint flags turned on. cout.setf(ios::showpoint); cout.setf(ios::showpos); cout << 100.0; // displays Θ You can OR together two or more flags,e.g: cout.setf(ios::showpoint ios::showpos); Clearing Format Flags ================= Θ The complement of setf() is unsetf() void unsetf(fmtflags flags);
3 Θ The flags specified by flags are cleared. Θ E.g Program: cout.setf(ios::uppercase ios::scientific); cout << ; // displays E+02 cout.unsetf(ios::uppercase); // clear uppercase cout << "\n" << ; // displays e+02 An Overloaded Form of setf( ) ====================== fmtflags setf( fmtflags flags1, fmtflags flags2); Θ only the flags specified by flags2 are affected. Θ They are first cleared and then set according to the flags specified by flags1. Θ previous flags setting is returned. int main( ) cout.setf(ios::showpoint ios::showpos, ios::showpoint); cout << 100.0; // displays , not Θ showpoint is set, but not showpos, since it is not specified in the second parameter. Θ most common use of the two-parameter form of setf( ) is when setting the number base, justification etc. Θ E.g: Θ references to the oct, dec, and hex fields can collectively be referred to as basefield. Θ flags that comprise these groupings are mutually exclusive, means, need to turn off one flag when setting another. Θ output in hexadecimal, some implementations require that the other number base flags be turned off. Θ E.g program
4 cout.setf(ios::hex, ios::basefield); cout << 100; // this displays 64 Θ basefield flags (i.,e., dec, oct, and hex) are first cleared and then the hex flag is set. Θ only the flags specified in flags2 can be affected by flags specified by flags1. cout.setf(ios::showpos, ios::hex); // error, showpos not set cout << 100 << '\n'; // displays 100, not +100 cout.setf(ios::showpos, ios::showpos); // this is correct cout << 100; // now displays +100 Θ In this program, the first attempt to set the showpos flag fails. Examining the Formatting Flags ========================= Θ To know the current format settings but not alter any. Θ For this, ios includes the member function flags() fmtflags flags(); Θ E.g program: void showflags() ; // show default condition of format flags showflags(); cout.setf(ios::right ios::showpoint ios::fixed); showflags(); // This function displays the status of the format flags. void showflags() long f; long i; f = (long) cout.flags(); // get flag settings
5 // check each flag for(i=0x4000; i; i = i >> 1) if(i & f) cout << "1 "; else cout << "0 "; cout << " \n"; Setting All Flags ============= fmtflags flags(fmtflags f); Θ allows to set all format flags associated with a stream. Θ the bit pattern found in f is used to set the format flags associated with the stream. void showflags(); // show default condition of format flags showflags(); // showpos, showbase, oct, right are on, others off ios::fmtflags f = ios::showpos ios::showbase ios::oct ios::right; cout.flags(f); // set all flags showflags(); width(), precision(), and fill() ====================== Θ three member functions defined by ios that set the format parameters: i) the field width ii) the precision, and iii) the fill character. Θ when a value is output, it occupies only as much space as the number of characters it takes to display it. However, you can specify a minimum field width by using the width( ) function. streamsize width(streamsize w); Θ w becomes the field width. Θ the previous field width is returned. Θ streamsize type is defined as some form of integer by the compiler. Θ In some implementations, the field width must be set before each output. Θ When outputting floating point values, you can determine the number of digits of precision by using the precision( ) function. streamsize precision( streamsize p);
6 Θ the precision is set to p, and the old value is returned. Θ when a value uses less than the specified width, the field will be padded with the current fill character(space characer, by default). Θ If the size of the value exceeds the minimum field width, the field will be overrun. Θ when a field needs to be filled, it is filled with spaces. Θ We can specify any other fill character by using the fill( ) function. char fill(char ch); Θ After a call to fill( ), ch becomes the new fill character. Θ old one is returned. E.g program: cout.precision(4) ; cout.width(10); cout << << "\n"; // displays : cout.fill('*'); cout.width(10); cout << << "\n"; // displays *****10.12 // field width applies to strings, too cout.width(10); cout << "Hi!" << "\n"; // displays: *******Hi! cout.width(10); cout.setf(ios::left); // left justify cout << ; // displays: 10.12***** output: *****10.12 *******Hi! 10.12***** Θ There are overloaded forms of width( ), precision( ), and fill( ) that obtain but do not change the current setting. char fill( ); streamsize width( ); streamsize precision( );
7 Using Manipulators to Format I/O ========================== Θ We can alter the format parameters of a stream is through the use of special functions called manipulators that can be included in an I/O expression. E.g: boolalpha Turns on boolapha flag. Input/Output dec Turns on dec flag. Input/Output hex Turns on hex flag. Input/Output endl Output a newline character Output and flush the stream. Θ setiosflags( ) manipulator to directly set the various format flags related to a stream. Θ setfill(int ch) Set the fill character to ch. #include <iomanip> cout << hex << 100 << endl; cout << setfill('?') << setw(10) << ; Output: 64??????2343 Θ main advantage of using manipulators instead of the ios member functions is that they often allow more compact code to be written. Θ Manipulator setiosflags( ) manipulator to directly set the various format flags related to a stream. #include <iomanip> cout << setiosflags(ios::showpos); cout << setiosflags(ios::showbase); cout << 123 << " " << hex << 123; Θ Manipulator boolapha allows true and false values to be input and output using the words "true" and "false" rather than numbers 1 or 0.
8 bool b; b = true; cout << b << " " << boolalpha << b << endl; cout << "Enter a Boolean value: "; cin >> boolalpha >> b; cout << "Here is what you entered: Output: 1 true Enter a Boolean value: false Here is what you entered: false C++ File I/O ========= Θ perform file I/O, you must include the header <fstream> Θ These classes are derived from istream, ostream, and iostream, Θ Remember, istream, ostream, and iostream are derived from ios, so ifstream, ofstream, and fstream also have access to all operations defined by ios. Fig: iostream class hierarchy Opening and Closing a File ==================== Θ In C++, you open a file by linking it to a stream. Before you can open a file, you must first obtain a stream. Θ There are three types of streams: input, output, and input/output. ifstream in; ofstream out; fstream io; // input // output // input and output void ifstream::open(const char *filename, ios::openmode mode = ios::in); void ofstream::open(const char *filename, ios::openmode mode = ios::out ios::trunc);
9 void fstream::open(const char *filename, ios::openmode mode = ios::in ios::out); Θ Second parameters is enumeration defined by ios class. Θ ios::app causes all output to that file to be appended to the end. Θ ios::in value specifies that the file is capable of input(read). Θ ios::out value specifies that the file is capable of output(write). Θ ios::trunc value causes the contents of a preexisting file by the same name to be destroyed. ofstream out; out.open("test", ios::out); Θ If open( ) fails, the stream will evaluate to false when used in a Boolean expression. out.open("test", ios::out); if(!out) cout << "Cannot open file.\n"; // handle error Θ Possible to open file by directly giving filename to the stream object when created. ifstream mystream("myfile"); // open file for input Θ check to see if you have successfully opened a file by using the is_open() Prototype: bool is_open( ); if(!out.is_open()) cout << "File is not open.\n"; //... Θ To close a file, use the member function close( ). out.close(); Θ close( ) function takes no parameters and returns no value. Reading and Writing Text Files ======================= Θ To read from or write to a text file, simply use the << and >> operators the same way you do when performing console I/O. // Reading and writing text file ofstream out("invntry"); // output, normal file
10 if(!out) cout << "Cannot open INVENTORY file.\n"; out << "Radios " << << endl; out << "Toasters " << << endl; out << "Mixers " << << endl; out.close(); Θ Following program reads the inventory file created by the previous program and displays its contents on the screen: ifstream in("invntry"); // input if(!in) cout << "Cannot open INVENTORY file.\n"; char item[20]; float cost; in >> item >> cost; cout << item << " " << cost << "\n"; in >> item >> cost; cout << item << " " << cost << "\n"; in >> item >> cost; cout << item << " " << cost << "\n"; in.close(); Θ Following is another example of disk I/O. This program reads strings entered at the keyboard and writes them to disk. Θ The program stops when the user enters an exclamation point(!). int main(int argc, char *argv[]) if(argc!=2)
11 cout << "Usage: output <filename>\n"; ofstream out(argv[1]); // output, normal file if(!out) cout << "Cannot open output file.\n"; char str[80]; cout << "Write strings to disk. Enter! to stop.\n"; do cout << ": "; cin >> str; out << str << endl; while (*str!= '!'); out.close(); When reading text files using the >> operator, keep in mind that certain character translations will occur. For example, white- space characters are omitted. Θ If you want to prevent any character translations, you must open a file for binary access and use the functions discussed in the next section. Unformatted and Binary I/O ===================== Θ there will be times when you need to store unformatted (raw) binary data, not text(executable files, audio/video files etc.) Θ There are functions to do this. Θ Though often we think character and byte are equivalent, it need not be always. Θ While byte is always 8 bit, a character need not be 8- bit(e.g wchat_t type in c++) put( ) and get( ) =========== istream &get(char &ch); ostream &put(char ch); Θ Used to read and write unformatted data. Θ these functions read and write bytes of data as characters. Θ get( ) function reads a single character from the invoking stream and puts that value in ch. Θ function put() writes ch to the stream and returns a reference to the stream. Θ The following program displays the contents of any file, whether it contains text or binary data, on the screen.
12 int main(int argc, char *argv[]) char ch; if(argc!=2) cout << "Usage: PR <filename>\n"; ifstream in(argv[1], ios::in ios::binary); if(!in) cout << "Cannot open file."; while(in) // in will be false when eof is reached in.get(ch); if(in) cout << ch; Θ when the end-of-file is reached, the stream associated with the file becomes false Θ compact way to code the loop that reads and displays a file. while(in.get(ch)) cout << ch; Θ Program writes into a file using put() int i; ofstream out("chars", ios::out ios::binary); if(!out) cout << "Cannot open output file.\n"; // write all characters to disk
13 for(i=0; i<256; i++) out.put((char) i); out.close(); read( ) and write( ) ============== Θ to read and write blocks of binary data. istream &read ( char *buf, streamsize num); ostream &write ( const char *buf, streamsize num); Θ read( ) function reads num characters from the invoking stream and puts them in the buffer pointed to by buf. Θ write( ) function writes num characters to the invoking stream from the buffer pointed to by buf. Θ program writes a structure to disk and then reads it back in: #include <cstring> struct status char name[80]; double balance; unsigned long account_num; ; struct status acc; strcpy(acc.name, "Ralph Trantor"); acc.balance = ; acc.account_num = ; // write data ofstream outbal("balance", ios::out ios::binary); if(!outbal) cout << "Cannot open file.\n"; outbal.write((char *) &acc, sizeof(struct status)); outbal.close();
14 // now, read back; ifstream inbal("balance", ios::in ios::binary); if(!inbal) cout << "Cannot open file.\n"; inbal.read((char *) &acc, sizeof(struct status)); cout << acc.name << endl; cout << "Account # " << acc.account_num; cout.precision(2); cout.setf(ios::fixed); cout << endl << "Balance: $" << acc.balance; inbal.close(); Θ If the end of the file is reached before num characters have been read, then read( ) simply stops, and the buffer contains as many characters as were available. Θ You can find out how many characters have been read by using another member function, called gcount( ), which has this prototype. streamsize gcount(); Θ program shows another example of read( ) and write( ) and illustrates the use of gcount( ): double fnum[4] = 99.75, , , 200.1; int i; ofstream out("numbers", ios::out ios::binary); if(!out) cout << "Cannot open file."; out.write((char *) &fnum, sizeof fnum); out.close(); for(i=0; i<4; i++) // clear array fnum[i] = 0.0; ifstream in("numbers", ios::in ios::binary); in.read((char *) &fnum, sizeof fnum); // see how many bytes have been read cout << in.gcount() << " bytes read\n"; for(i=0; i<4; i++) // show values read from file cout << fnum[i] << " ";
15 in.close(); More get( ) Functions ================ Θ More overloads for get function istream &get(char *buf, streamsize num); istream &get(char *buf, streamsize num, char delim); int get( ); Θ first form reads characters into the array pointed to by buf until either num- 1 characters have been read, a newline is found, or the end of the file has been encountered. Θ newline character is encountered in the input stream, it is not extracted. Instead, it remains in the stream until the next input operation. Θ second form reads characters into the array pointed to by buf until either num- 1 characters have been read, the character specified by delim has been found, or the end of the file has been encountered. Θ The array pointed to by buf will be null terminated by get( ). Θ If the delimiter character is encountered in the input stream, it is not extracted. Instead, it remains in the stream until the next input operation. Θ third overloaded form of get( ) returns the next character from the stream. Θ It returns EOF if the end of the file is encountered. Θ This form of get( ) is similar to C's getc( ) function. getline() ====== istream& getline(char *buf, streamsize num); istream& getline(char *buf, streamsize num, char delim); Θ first form reads characters into the array pointed to by buf until either num - 1 characters have been read, a newline character has been found, or the end of the file has been encountered. Θ The array pointed to by buf will be null terminated by getline( ). If the newline character is encountered in the input stream, it is extracted, but is not put into buf. Θ second form reads characters into the array pointed to by buf until either num-1 characters have been read, the character specified by delim has been found, or the end of the file has been encountered. Θ The array pointed to by buf will be null terminated by getline( ). If the delimiter character is encountered in the input stream, it is extracted, but is not put into buf.
16 Getline example program: // Read and display a text file line by line. int main(int argc, char *argv[]) if(argc!=2) cout << "Usage: Display <filename>\n"; ifstream in(argv[1]); // input if(!in) cout << "Cannot open input file.\n"; char str[255]; while(in) in.getline(str, 255); // delim defaults to '\n' if(in) cout << str << endl; in.close(); Detecting EOF =========== Θ can detect when the end of the file is reached by using the member function eof(). bool eof( ); Θ returns true when the end of the file has been reached; otherwise it returns false. /* Display contents of specified file in both ASCII and in hex. */ #include <cctype> #include <iomanip> int main(int argc, char *argv[]) if(argc!=2) cout << "Usage: Display <filename>\n";
17 ifstream in(argv[1], ios::in ios::binary); if(!in) cout << "Cannot open input file.\n"; register int i, j; int count = 0; char c[16]; cout.setf(ios::uppercase); while(!in.eof()) for(i=0; i<16 &&!in.eof(); i++) in.get(c[i]); if(i<16) i--; // get rid of eof for(j=0; j<i; j++) cout << setw(3) << hex << (int) c[j]; for(; j<16; j++) cout << " "; cout << "\t"; for(j=0; j<i; j++) if(isprint(c[j])) cout << c[j]; else cout << "."; cout << endl; count++; if(count==16) count = 0; cout << "Press ENTER to continue: "; cin.get(); cout << endl; in.close(); The ignore( ) Function ================= Θ Used to read and discard characters from the input stream.
18 istream& ignore( streamsize num=1, int_type delim=eof); Θ reads and discards characters until either num characters have been ignored (1 by default) or the character specified by delim is encountered (EOF by default). Θ int_type is defined as some form of integer. Θ program reads a file called TEST. It ignores characters until either a space is encountered or 10 characters have been read. It then displays the rest of the file. ifstream in("test"); if(!in) cout << "Cannot open file.\n"; /* Ignore up to 10 characters or until first space is found. */ in.ignore(10, ' '); char c; while(in) in.get(c); if(in) cout << c; in.close(); peek() and putback() =============== int_type peek( ); Θ obtain the next character in the input stream without removing it from that stream. Θ returns the next character in the stream or EOF if the end of the file is encountered. istream& putback(char c); Θ return the last character read from a stream to that stream. flush() ===== ostream & flush( );
19 Θ When output is performed, data is not necessarily immediately written to the physical device linked to the stream. Θ Instead, information is stored in an internal buffer until the buffer is full. Θ you can force the information to be physically written to disk before the buffer is full by calling flush( ). Θ Closing a file or terminating a program also flushes all buffers. SLE: Random Access (to file content): Functions: seekg & seekp, examples EOF
CS2141 Software Development using C/C++ Stream I/O
CS2141 Software Development using C/C++ Stream I/O iostream Two libraries can be used for input and output: stdio and iostream The iostream library is newer and better: It is object oriented It can make
More informationModule 11 The C++ I/O System
Table of Contents Module 11 The C++ I/O System CRITICAL SKILL 11.1: Understand I/O streams... 2 CRITICAL SKILL 11.2: Know the I/O class hierarchy... 3 CRITICAL SKILL 11.3: Overload the > operators...
More informationUNIT V FILE HANDLING
UNIT V CONTENTS: Streams and formatted I/O I/O manipulators File handling Random access Object serialization Namespaces Std namespace ANSI String Objects Standard template library FILE HANDLING Streams:
More informationUEE1303(1070) S 12 Object-Oriented Programming in C++
Computational Intelligence on Automation Lab @ NCTU Learning Objectives UEE1303(1070) S 12 Object-Oriented Programming in C++ Lecture 06: Streams and File Input/Output I/O stream istream and ostream member
More informationStreams - Object input and output in C++
Streams - Object input and output in C++ Dr. Donald Davendra Ph.D. Department of Computing Science, FEI VSB-TU Ostrava Dr. Donald Davendra Ph.D. (Department of Computing Streams - Object Science, input
More informationChapter 21 - C++ Stream Input/Output
Chapter 21 - C++ Stream Input/Output Outline 21.1 Introduction 21.2 Streams 21.2.1 Iostream Library Header Files 21.2.2 Stream Input/Output Classes and Objects 21.3 Stream Output 21.3.1 Stream-Insertion
More informationChapter 21 - C++ Stream Input/Output
Chapter 21 - C++ Stream Input/Output Outline 21.1 Introduction 21.2 Streams 21.2.1 Iostream Library Header Files 21.2.2 Stream Input/Output Classes and Objects 21.3 Stream Output 21.3.1 Stream-Insertion
More informationChapter 12 - C++ Stream Input/Output
Chapter 12 - C++ Stream Input/Output 1 12.1 Introduction 12.2 Streams 12.2.1 Classic Streams vs. Standard Streams 12.2.2 iostream Library Header Files 12.2.3 Stream Input/Output Classes and Objects 12.3
More informationPiyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class
C++ IO C++ IO All I/O is in essence, done one character at a time For : COP 3330. Object oriented Programming (Using C++) http://www.compgeom.com/~piyush/teach/3330 Concept: I/O operations act on streams
More informationC++ Programming Lecture 10 File Processing
C++ Programming Lecture 10 File Processing By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department Outline Introduction. The Data Hierarchy. Files and Streams. Creating a Sequential
More informationI/O Streams and Standard I/O Devices (cont d.)
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 from the standard input device Learn how to use predefined
More informationbasic_fstream<chart, traits> / \ basic_ifstream<chart, traits> basic_ofstream<chart, traits>
The C++ I/O System I/O Class Hierarchy (simplified) ios_base ios / \ istream ostream \ / iostream ifstream fstream ofstream The class ios_base -- public variables and methods The derived classes istream,
More informationC++ Input/Output: Streams
C++ Input/Output: Streams Basic I/O 1 The basic data type for I/O in C++ is the stream. C++ incorporates a complex hierarchy of stream types. The most basic stream types are the standard input/output streams:
More informationby Pearson Education, Inc. All Rights Reserved. 2
The C++ standard libraries provide an extensive set of input/output capabilities. C++ uses type-safe I/O. Each I/O operation is executed in a manner sensitive to the data type. If an I/O member function
More informationLab 6. Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA
Variables: Lab 6 Review of Variables, Formatting & Loops By: Dr. John Abraham, Professor, UTPA We learned that a variable is a name assigned to the first byte of the necessary memory to store a value.
More informationCS201 Solved MCQs.
15.1 Answer each of the following: a. Input/output in C++ occurs as of bytes. b. The stream manipulators that format justification are, and. c. Member function can be used to set and reset format state.
More informationChapter Overview. I/O Streams as an Introduction to Objects and Classes. I/O Streams. Streams and Basic File I/O. Objects
Chapter 6 I/O Streams as an Introduction to Objects and Classes Overview 6.1 Streams and Basic File I/O 6.2 Tools for Stream I/O 6.3 Character I/O Copyright 2008 Pearson Addison-Wesley. All rights reserved.
More informationConvenient way to deal large quantities of data. Store data permanently (until file is deleted).
FILE HANDLING Why to use Files: Convenient way to deal large quantities of data. Store data permanently (until file is deleted). Avoid typing data into program multiple times. Share data between programs.
More informationChapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 6 I/O Streams as an Introduction to Objects and Classes Overview 6.1 Streams and Basic File I/O 6.2 Tools for Stream I/O 6.3 Character I/O Slide 6-3 6.1 Streams and Basic File I/O I/O Streams I/O
More informationChapter 12. Streams and File I/O. Copyright 2010 Pearson Addison-Wesley. All rights reserved
Chapter 12 Streams and File I/O Copyright 2010 Pearson Addison-Wesley. All rights reserved Learning Objectives I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output,
More informationChapter 12. Streams and File I/O. Copyright 2016 Pearson, Inc. All rights reserved.
Chapter 12 Streams and File I/O Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output, flag
More informationObjects and streams and files CS427: Elements of Software Engineering
Objects and streams and files CS427: Elements of Software Engineering Lecture 6.2 (C++) 10am, 13 Feb 2012 CS427 Objects and streams and files 1/18 Today s topics 1 Recall...... Dynamic Memory Allocation...
More informationIS 0020 Program Design and Software Tools
1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005 Introduction 2 Storage of data Arrays, variables are temporary Files are permanent Magnetic disk, optical
More informationFile I/O Christian Schumacher, Info1 D-MAVT 2013
File I/O Christian Schumacher, chschuma@inf.ethz.ch Info1 D-MAVT 2013 Input and Output in C++ Stream objects Formatted output Writing and reading files References General Remarks I/O operations are essential
More informationStreams in C++ Stream concept. Reference information. Stream type declarations
Stream concept A stream represent a sequence of bytes arriving, being retrieved, being stored, or being sent, in order. A stream is continuos and offer sequential access to the data. Each byte can be read
More informationObject Oriented Programming In C++
C++ Question Bank Page 1 Object Oriented Programming In C++ 1741059 to 1741065 Group F Date: 31 August, 2018 CIA 3 1. Briefly describe the various forms of get() function supported by the input stream.
More informationWe will exclusively use streams for input and output of data. Intro Programming in C++
C++ Input/Output: Streams The basic data type for I/O in C++ is the stream. C++ incorporates a complex hierarchy of stream types. The most basic stream types are the standard input/output streams: 1 istream
More informationIntroduction to Standard C++ Console I/O. C++ Object Oriented Programming Pei-yih Ting NTOU CS
Introduction to Standard C++ Console I/O C++ Object Oriented Programming Pei-yih Ting NTOU CS 1 Contents I/O class hierarchy, cin, cout > operators Buffered I/O cin.get() and cin.getline() status
More informationUNIT V FILE HANDLING
UNIT V CONTENTS: Streams and formatted I/O I/O manipulators File handling Random access Object serialization Namespaces Std namespace ANSI String Objects Standard template library FILE HANDLING Streams:
More informationChapter 3 - Notes Input/Output
Chapter 3 - Notes Input/Output I. I/O Streams and Standard I/O Devices A. I/O Background 1. Stream of Bytes: A sequence of bytes from the source to the destination. 2. 2 Types of Streams: i. Input Stream:
More informationChapter 12. Streams and File I/O
Chapter 12 Streams and File I/O Learning Objectives I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output, flag settings Introduction Streams Special objects Deliver
More informationUnit-V File operations
Unit-V File operations What is stream? C++ IO are based on streams, which are sequence of bytes flowing in and out of the programs. A C++ stream is a flow of data into or out of a program, such as the
More informationStreams. Parsing Input Data. Associating a File Stream with a File. Conceptual Model of a Stream. Parsing. Parsing
Input Data 1 Streams 2 Streams Conceptual Model of a Stream Associating a File Stream with a File Basic Stream Input Basic Stream Output Reading Single Characters: get() Skipping and Discarding Characters:
More informationStreams. Rupesh Nasre.
Streams Rupesh Nasre. OOAIA January 2018 I/O Input stream istream cin Defaults to keyboard / stdin Output stream ostream cout std::string name; std::cout > name; std::cout
More informationObject Oriented Programming Using C++ UNIT-3 I/O Streams
File - The information / data stored under a specific name on a storage device, is called a file. Stream - It refers to a sequence of bytes. Text file - It is a file that stores information in ASCII characters.
More informationChapter 14 Sequential Access Files
Chapter 14 Sequential Access Files Objectives Create file objects Open a sequential access file Determine whether a sequential access file was opened successfully Write data to a sequential access file
More informationStandard I/O in C and C++
Introduction to Computer and Program Design Lesson 7 Standard I/O in C and C++ James C.C. Cheng Department of Computer Science National Chiao Tung University Standard I/O in C There three I/O memory buffers
More informationC++ Basics. Lecture 2 COP 3014 Spring January 8, 2018
C++ Basics Lecture 2 COP 3014 Spring 2018 January 8, 2018 Structure of a C++ Program Sequence of statements, typically grouped into functions. function: a subprogram. a section of a program performing
More informationEngineering Problem Solving with C++, Etter/Ingber
Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input
More informationLecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-
Lecture 3 Input and Output Review from last week Variable - place to store data in memory identified by a name should be meaningful Has a type- int double char bool Has a value may be garbage change value
More informationPhysics 6720 I/O Methods October 30, C++ and Unix I/O Streams
Physics 6720 I/O Methods October 30, 2002 We have been using cin and cout to handle input from the keyboard and output to the screen. In these notes we discuss further useful capabilities of these standard
More informationChapte t r r 9
Chapter 9 Session Objectives Stream Class Stream Class Hierarchy String I/O Character I/O Object I/O File Pointers and their manipulations Error handling in Files Command Line arguments OOPS WITH C++ Sahaj
More informationIntroduction to C++ (Extensions to C)
Introduction to C++ (Extensions to C) C is purely procedural, with no objects, classes or inheritance. C++ is a hybrid of C with OOP! The most significant extensions to C are: much stronger type checking.
More informationStreams and Basic File I/O Tools for Stream I/O Character I/O Inheritance
Chapter 6 In this chapter, you will learn about: Streams and Basic File I/O Tools for Stream I/O Character I/O Inheritance Streams and Basic File I/O I refers to the program Input O refers to program Output:
More informationToday in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1
Today in CS162 External Files What is an external file? How do we save data in a file? CS162 External Data Files 1 External Files So far, all of our programs have used main memory to temporarily store
More informationDeveloped By : Ms. K. M. Sanghavi
Developed By : Ms. K. M. Sanghavi Stream and files Stream Classes Stream Errors Disk File I/O with Streams, Manipulators File I/O Streams with Functions Error Handling in File Overloading the Extraction
More informationIBM. C/C++ Legacy Class Libraries Reference SC
IBM C/C++ Legacy Class Libraries Reference SC09-7652-02 IBM C/C++ Legacy Class Libraries Reference SC09-7652-02 Note! Before using this information and the product it supports, read the information in
More informationFall 2017 CISC/CMPE320 9/27/2017
Notices: CISC/CMPE320 Today File I/O Text, Random and Binary. Assignment 1 due next Friday at 7pm. The rest of the assignments will also be moved ahead a week. Teamwork: Let me know who the team leader
More informationInput and Output. Data Processing Course, I. Hrivnacova, IPN Orsay
Input and Output Data Processing Course, I. Hrivnacova, IPN Orsay Output to the Screen Input from the Keyboard IO Headers Output to a File Input from a File Formatting I. Hrivnacova @ Data Processing Course
More informationModule C++ I/O System Basics
1 Module - 36 C++ I/O System Basics Table of Contents 1. Introduction 2. Stream classes of C++ 3. Predefined Standard Input/Output Streams 4. Functions of class 5. Functions of class
More informationStrings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter
More informationConsider the following example where a base class has been derived by other two classes:
Class : BCA 3rd Semester Course Code: BCA-S3-03 Course Title: Object Oriented Programming Concepts in C++ Unit IV Polymorphism The word polymorphism means having many forms. Typically, polymorphism occurs
More informationReview for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure
Computer Systems Computer System Computer Structure C++ Environment Imperative vs. object-oriented programming in C++ Input / Output Primitive data types Software Banking System Compiler Music Player Text
More informationJava IO and C++ Streams
Java IO and C++ Streams October 22, 2004 Operator Overloading in C++ - 2004-10-21 p. 1/31 Outline Java IO InputStream/OutputStream FilterInputStream/FilterOutputStream DataInputStream/DataOutputStream
More informationLecture 5 Files and Streams
Lecture 5 Files and Streams Introduction C programs can store results & information permanently on disk using file handling functions These functions let you write either text or binary data to a file,
More information10/23/02 21:20:33 IO_Examples
1 Oct 22 22:07 2000 extractor1.c Page 1 istream &operator>>( istream &in, Point &p ){ char junk; in >> junk >> p.x >> junk >> p.y >> junk; return in; 2 Oct 22 22:07 2000 extractor2.c Page 1 istream &operator>>(
More informationWriting a Good Program. 7. Stream I/O
Writing a Good Program 1 Input and Output I/O implementation is hardware dependent C++ does not, as a part of the language, define how data are sent out and read into the program The input and output (I/O)
More informationThe C++ Language. Output. Input and Output. Another type supplied by C++ Very complex, made up of several simple types.
The C++ Language Input and Output Output! Output is information generated by a program.! Frequently sent the screen or a file.! An output stream is used to send information. Another type supplied by C++
More informationChapter 8 File Processing
Chapter 8 File Processing Outline 1 Introduction 2 The Data Hierarchy 3 Files and Streams 4 Creating a Sequential Access File 5 Reading Data from a Sequential Access File 6 Updating Sequential Access Files
More informationCOMP322 - Introduction to C++
COMP322 - Introduction to C++ Lecture 05 - I/O using the standard library, stl containers, stl algorithms Dan Pomerantz School of Computer Science 5 February 2013 Basic I/O in C++ Recall that in C, we
More informationC++ does not, as a part of the language, define how data are sent out and read into the program
Input and Output C++ does not, as a part of the language, define how data are sent out and read into the program I/O implementation is hardware dependent The input and output (I/O) are handled by the standard
More informationAdvanced I/O Concepts
Advanced Object Oriented Programming Advanced I/O Concepts Seokhee Jeon Department of Computer Engineering Kyung Hee University jeon@khu.ac.kr 1 1 Streams Diversity of input sources or output destinations
More informationUNIT-5. When a C++ program begins execution, four built-in streams are automatically opened. They are: Stream Meaning Default Device
UNIT-5 C++ Streams - Like the C-based I/O system, the C++ I/O system operates through streams. A stream is a logical device that either produces or consumes information. A stream is linked to a physical
More informationC++ files and streams. Lec 28-31
C++ files and streams Lec 28-31 Introduction So far, we have been using the iostream standard library, which provides cin and cout methods for reading from standard input and writing to standard output
More informationIntroduction. Lecture 5 Files and Streams FILE * FILE *
Introduction Lecture Files and Streams C programs can store results & information permanently on disk using file handling functions These functions let you write either text or binary data to a file, and
More informationWhat we will learn about this week:
What we will learn about this week: Streams Basic file I/O Tools for Stream I/O Manipulators Character I/O Get and Put EOF function Pre-defined character functions Objects 1 I/O Streams as an Introduction
More informationC++ Binary File I/O. C++ file input and output are typically achieved by using an object of one of the following classes:
C++ Binary File I/O C++ file input and output are typically achieved by using an object of one of the following classes: ifstream for reading input only. ofstream for writing output only. fstream for reading
More informationEP241 Computing Programming
EP241 Computing Programming Topic 9 File Management Department of Engineering Physics University of Gaziantep Course web page www.gantep.edu.tr/~bingul/ep241 Sep 2013 Sayfa 1 Overview of Streams in C++
More informationMore File Operations. Lecture 17 COP 3014 Spring april 18, 2018
More File Operations Lecture 17 COP 3014 Spring 2018 april 18, 2018 eof() member function A useful member function of the input stream classes is eof() Stands for end of file Returns a bool value, answering
More informationCSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]
CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1] LEARNING OBJECTIVES Upon completion, you should be able to: o define C++ text files o explain the benefits of using I/O file processing o explain
More informationLecture 9. Introduction
Lecture 9 File Processing Streams Stream I/O template hierarchy Create, update, process files Sequential and random access Formatted and raw processing Namespaces Lec 9 Programming in C++ 1 Storage of
More informationDeveloped By : Ms. K. M. Sanghavi
Developed By : Ms. K. M. Sanghavi Designing Our Own Manipulators We can design our own manipulators for certain special purpose.the general form for creating a manipulator without any arguments is: ostream
More informationCOMP322 - Introduction to C++
COMP322 - Introduction to C++ Winter 2011 Lecture 05 - I/O using the standard library & Introduction to Classes Milena Scaccia School of Computer Science McGill University February 1, 2011 Final note on
More informationInput/Output Streams: Customizing
DM560 Introduction to Programming in C++ Input/Output Streams: Customizing Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark [Based on slides by Bjarne Stroustrup]
More informationProgramming II with C++ (CSNB244) Lab 10. Topics: Files and Stream
Topics: Files and Stream In this lab session, you will learn very basic and most common I/O operations required for C++ programming. The second part of this tutorial will teach you how to read and write
More informationSetting Justification
Setting Justification Formatted I/O 1 Justification - Justification refers to the alignment of data within a horizontal field. - The default justification in output fields is to the right, with padding
More informationChapter-12 DATA FILE HANDLING
Chapter-12 DATA FILE HANDLING Introduction: A file is a collection of related data stored in a particular area on the disk. Programs can be designed to perform the read and write operations on these files.
More informationWeek 3: File I/O and Formatting 3.7 Formatting Output
Week 3: File I/O and Formatting 3.7 Formatting Output Formatting: the way a value is printed: Gaddis: 3.7, 3.8, 5.11 CS 1428 Fall 2014 Jill Seaman spacing decimal points, fractional values, number of digits
More informationCSE 100: STREAM I/O, BITWISE OPERATIONS, BIT STREAM I/O
CSE 100: STREAM I/O, BITWISE OPERATIONS, BIT STREAM I/O PA2: encoding/decoding ENCODING: 1.Scan text file to compute frequencies 2.Build Huffman Tree 3.Find code for every symbol (letter) 4.Create new
More informationVuZs Team's Work. CS201 Spring Solved by vuzs Team with Reference Written by Administrator Wednesday, 19 May :52
CS201 Spring2009 5 Solved by vuzs Team with Reference Written by Administrator Wednesday, 19 May 2010 17:52 MIDTERM EXAMINATION Spring 2009 CS201- Introduction to Programming Shared & Solved by vuzs Team
More informationString Variables and Output/Input. Adding Strings and Literals to Your Programming Skills and output/input formatting
String Variables and Output/Input Adding Strings and Literals to Your Programming Skills and output/input formatting A group of characters put together to create text is called a string. Strings are one
More informationChapter 15 - C++ As A "Better C"
Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference
More informationFile Operations. Lecture 16 COP 3014 Spring April 18, 2018
File Operations Lecture 16 COP 3014 Spring 2018 April 18, 2018 Input/Ouput to and from files File input and file output is an essential in programming. Most software involves more than keyboard input and
More informationOBJECT ORIENTED DESIGN WITH C++ AN INTRODUCTION
OBJECT ORIENTED DESIGN WITH C++ AN INTRODUCTION Software is a collection of programs. Program is a set of statements that performs a specific task. Since the invention of the computer, many programming
More informationFundamentals of Programming Session 27
Fundamentals of Programming Session 27 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines
More informationThe C++ Input/Output Class Hierarchy
C++ Programming: The C++ Input/Output Class Hierarchy 2018 년도 2 학기 Instructor: Young-guk Ha Dept. of Computer Science & Engineering Contents Basics on C++ I/O The I/O class hierarchy The common base I/O
More informationFormatting outputs String data type Interactive inputs File manipulators. Access to a library that defines 3. instead, a library provides input
Input and Output Outline Formatting outputs String data type Interactive inputs File manipulators CS 1410 Comp Sci with C++ Input and Output 1 CS 1410 Comp Sci with C++ Input and Output 2 No I/O is built
More informationThis chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.
C++ PROGRAMMING LANGUAGE: NAMESPACE AND MANGEMENT OF INPUT/OUTPUT WITH C++. CAAM 519, CHAPTER 15 This chapter introduces the notion of namespace. We also describe how to manage input and output with C++
More informationInput and Output File (Files and Stream )
Input and Output File (Files and Stream ) BITE 1513 Computer Game Programming Week 14 Scope Describe the fundamentals of input & output files. Use data files for input & output purposes. Files Normally,
More informationStreams contd. Text: Chapter12, Big C++
Streams contd pm_jat@daiict.ac.in Text: Chapter12, Big C++ Streams Objects are Abstracted Wrapper around input/output source/destinations Steps in reading/writing streams- Open: Establish connection between
More informationcs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.
cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... today: language basics: identifiers, data types, operators, type conversions, branching and looping, program structure
More informationAdvanced File Operations. Review of Files. Declaration Opening Using Closing. CS SJAllan Chapter 12 2
Chapter 12 Advanced File Operations Review of Files Declaration Opening Using Closing CS 1410 - SJAllan Chapter 12 2 1 Testing for Open Errors To see if the file is opened correctly, test as follows: in.open("cust.dat");
More informationRandom File Access. 1. Random File Access
Random File Access 1. Random File Access In sequential file access, the file is read or written sequentially from the beginning. In random file access, you can skip around to various points in the file
More informationby Pearson Education, Inc. All Rights Reserved. 2
Data that is formatted and written to a sequential file as shown in Section 17.4 cannot be modified without the risk of destroying other data in the file. For example, if the name White needs to be changed
More informationGenerate error the C++ way
Reference informa9on Lecture 3 Stream I/O Consult reference for complete informa9on! UNIX man- pages (available on exam): man topic man istream man ostream ios, basic_string, stringstream, ctype, numeric_limits
More informationBasic function of I/O system basics & File Processing
x 77 Basic function of I/O system basics & File Processing Syllabus Stream classes, using formatted & unformatted functions, using manipulator to format I/O, Basics of file system, opening & closing a
More informationChapter 3 : Assignment and Interactive Input (pp )
Page 1 of 50 Printer Friendly Version User Name: Stephen Castleberry email Id: scastleberry@rivercityscience.org Book: A First Book of C++ 2007 Cengage Learning Inc. All rights reserved. No part of this
More informationc++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.
Chapter 1 File Extensions: Source code (cpp), Object code (obj), and Executable code (exe). Preprocessor processes directives and produces modified source Compiler takes modified source and produces object
More informationGetting started with C++ (Part 2)
Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output
More informationInput/output. Remember std::ostream? std::istream std::ostream. std::ostream cin std::istream. namespace std { class ostream { /*...
Input/output Remember std::ostream? namespace std { class ostream { /*... */ }; } extern istream cin; extern ostream cout; extern ostream cerr; extern ostream clog; 7 / 24 std::istream std::ostream std
More information