320 likes | 340 Views
Character I/O Stream and File I/O Function Overloading. INFSY 535 Spring 2003 Lecture 7. Data. All data is input and output as character data. The number 15 is really ‘1’ and ‘5’ character data. Data. What does the command cin>> do?
E N D
Character I/O Stream and File I/O Function Overloading INFSY 535 Spring 2003 Lecture 7
Data • All data is input and output as character data. • The number 15 is really • ‘1’ and ‘5’ • character data.
Data • What does the command cin>> do? • extraction operator skips any leading whitespace characters • extracts the desired data value from the input stream • char data extracts a single character • numeric data extracts up to the first character that is inappropriate for the data type • leaves the reading marker on the character following the last piece of data extracted
Data • Statement Data Contents After Input • cin >> i; 32 i = ? • int i; • char ch; • double x; • cin >> i >> ch >> x; 25A16.9 i = ? • ch = ? • x = ?
Character I/O • Use special, low level commands to input and output data • eliminate all automatic conversions • eliminate skipping over whitespaces, newline characters • treats all data as type char • Read one character at a time • Check data validity
Character I/O • get and put member functions are a part of all stream definitions • <iostream> and <ifstream> • Read the very next character: • char next; • cin.get(next);
cin>> vs. cin.get(ch) • Data to be Read • A B\n • CD\n • StatementContents After Input • cin >> ch1; ch1 = ‘A’ • cin >> ch2; ch2 = ‘B’ • cin >> ch3; ch3 = ‘C’
cin>> vs. cin.get(ch) • Data to be Read • A B\n • CD\n • StatementContents After Input • cin.get(ch1); ch1 = ‘A’ • cin.get(ch2); ch2 = ‘ ’ • cin.get(ch3); ch3 = ‘B’
Character I/O • Caution - characters are letters, numbers, and all special characters including spaces, “\n”, etc. • Your program can detect end-of-line, spaces, end-of-file, etc. • Data can be checked for validity • It is wise to obtain the first character with cin>>chname; this skips spaces and special characters that may be before first character
Character I/O • This statement is used to output a single character to a device • char next; • cout.put(next);
Character I/O • We often ask users to enter numbers • Users often make errors on data entry • The system is unhappy when it receives alphabetic special characters in a numeric file so we must validate data
Files • It is much easier to store large quantities of data on a disk. • Batch Processing – reading data from an external file instead of through user interaction. • Hard Copy – output to paper
External Files • Program takes input from a file • “reading from the file” • Program sends output to a file • “writing to the file”
File I/O • Files are attached to streams • the type for input-file stream variables is ifstream • the type for output-file stream variables isofstream • these types are defined in the library<fstream>
How to Proceed • Associate a file with your program: • #include <fstream> • #define <filename> “external file name” • #define infile “test.txt” • #define outfile “report.txt”
How to Proceed • Link a type to a stream • ifstream ins;//associate ins with the input stream • ofstream out;//associate out with the output //stream
How to Proceed • Open the file • ins.open(infile); • out.open(outfile);
Processing Note • Output file processing: • if the file does not already exist, open will create the file • if the output file already exists, the open function will discard the contents of the file
Alternative • You could combine the definition and open (as is done in text) • ifstream ins; • ins.open(“test.txt”); • Note: in this case, you still must associate ins with ifstream
How to Proceed • Check to see that your file opened (closed) successfully • if (ins.fail( )) • { • cerr << “**Error on Open**” << endl; • return EXIT_FAILURE; • }
How to Proceed • EXIT_FAILURE is symbolic of an unsuccessful open • EXIT_SUCCESS is symbolic of a successful open
How to Proceed • Close your file • ins.close ( ); • out.close ( );
File Processing • Input from the file is accomplished using the extraction operator >> • ins >> number1 >> number2;
File Processing • Reading to end-of-file (eof) • while(ins >> next) • { • sum = sum + next; • count++; • } • ins >> next; • ORwhile(! ins.eof( )) • { • sum = sum + next; • count++; • ins >> next; • }
Formatting I/O • You may use formatting commands with any output stream • out << fixed << showpoint; • out << setprecision(2);
Function Overloading Function Overloading is the ability to declare functions of the same name with different sets of parameters 1) C++ selects the function by examining a) number of arguments b) types of the argument variables c) order of the arguments 2) Used to create several functions with the same name that perform a similar task but with different data types 3) Improves program readability for closely related tasks
Function Overloading void sum (intj) { j + = 5; cout << j << endl; return; } void sum(floatj) { j + = 5.1; cout << j << endl; return; }
Function Overloading void main ( ) { int x = 5; float y = 3.5; sum (x); sum (y); return; }
Polymorphism The use of the same function name to mean different things. The word is derived from Greek words meaning “many forms.” Compiler looks at the function definition, the number of parameters and types, to determine which one to use.
void read_and_clean (int, ifstream&, double&); void read_and_clean (int, ifstream&, int&); int read_and_clean (int, ifstream&, char [ ]);