1 / 32

Character I/O Stream and File I/O Function Overloading

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?

rstephenson
Download Presentation

Character I/O Stream and File I/O Function Overloading

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Character I/O Stream and File I/O Function Overloading INFSY 535 Spring 2003 Lecture 7

  2. Data • All data is input and output as character data. • The number 15 is really • ‘1’ and ‘5’ • character data.

  3. 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

  4. 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 = ?

  5. 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

  6. 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);

  7. 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’

  8. 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’

  9. 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

  10. Character I/O • This statement is used to output a single character to a device • char next; • cout.put(next);

  11. 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

  12. Stream and File I/O

  13. 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

  14. External Files • Program takes input from a file • “reading from the file” • Program sends output to a file • “writing to the file”

  15. 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>

  16. How to Proceed • Associate a file with your program: • #include <fstream> • #define <filename> “external file name” • #define infile “test.txt” • #define outfile “report.txt”

  17. 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

  18. How to Proceed • Open the file • ins.open(infile); • out.open(outfile);

  19. 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

  20. 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

  21. How to Proceed • Check to see that your file opened (closed) successfully • if (ins.fail( )) • { • cerr << “**Error on Open**” << endl; • return EXIT_FAILURE; • }

  22. How to Proceed • EXIT_FAILURE is symbolic of an unsuccessful open • EXIT_SUCCESS is symbolic of a successful open

  23. How to Proceed • Close your file • ins.close ( ); • out.close ( );

  24. File Processing • Input from the file is accomplished using the extraction operator >> • ins >> number1 >> number2;

  25. 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; • }

  26. Formatting I/O • You may use formatting commands with any output stream • out << fixed << showpoint; • out << setprecision(2);

  27. Function Overloading

  28. 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

  29. Function Overloading void sum (intj) { j + = 5; cout << j << endl; return; } void sum(floatj) { j + = 5.1; cout << j << endl; return; }

  30. Function Overloading void main ( ) { int x = 5; float y = 3.5; sum (x); sum (y); return; }

  31. 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.

  32. void read_and_clean (int, ifstream&, double&); void read_and_clean (int, ifstream&, int&); int read_and_clean (int, ifstream&, char [ ]);

More Related