C++ For Science and Engineering Lecture 14 John Chrispell Tulane University Monday September 27, 2010
File Input and Output Recall writing text to standard out You must include the iostream header file. The iostream header file declares an ostream variable, or object, called cout. You must account for the std namespace. (Use the using namespace directive, or the std:: prefix for elements such as cout and endl). You can use cout with the >> operator to read a variety of data types. John Chrispell, Monday September 27, 2010 slide 3/17
File Input and Output File output parallels this. You must include the fstream header file. The fstream header file defines an ofstream class for handling output. You need to declare one or more ofstream variables or objects, which you may name as you wish. You must account for the std namespace. This can be done using the namespace directive or using the std:: prefix for elements such as ofstream. You need to associate a specific ofstream object with a specific file. One way is to use the open() method. When your done you close the file using the close() method. You can use the >> operator with the ofstream object to output a variety of data types. John Chrispell, Monday September 27, 2010 slide 5/17
File Input and Output The syntax is as follows: o f s t r e a m o u t F i l e ; // o u t F i l e an o f s t r e a m o b j e c t o f s t r e a m f o u t ; // a second o f s t r e a m ; / a s s o c i a t e the stream with a f i l e / o u t F i l e. open ( M y f i l e. t x t ) ; // o u t F i l e can now be used l / a second way below / c i n >> f i l e n a m e ; f o u t. open ( f i l e n a m e ) ; / G i v e s a s p e c f i c out f i l e name / Consider the following example: John Chrispell, Monday September 27, 2010 slide 7/17
outfile.cpp #i n c lude <iostream > #i n c lude <fstream > // f o r f i l e I /O i n t main ( ) { using namespace std ; char automobile [ 5 0 ] ; i n t year ; double a p r i c e ; double d p r i c e ; ofstream o u t F i l e ; // create object for output o u t F i l e. open ( c a r i n f o. txt ) ; // a s s o c i a t e with a f i l e cout << Enter the make and model of automobile : ; c i n. g e t l i n e ( automobile, 5 0 ) ; cout << Enter the model year : ; c i n >> year ; cout << Enter the o r i g i n a l asking p r i c e : ; cin >> a p r i c e ; d p r i c e = 0.913 a p r i c e ; / ======================================= / / d i s p l a y i n f o r m a t i o n on s c r e e n with cout / / ======================================= / cout << f i x e d ; cout. p r e c i s i o n ( 2 ) ; cout. s e t f ( i o s b a s e : : showpoint ) ; cout << Make and model : << automobile << endl ; cout << Year : << y e a r << e n d l ; cout << Was asking $ << a p r i c e << endl ; cout << Now asking $ << d p r i c e << endl ; / ====================================================== / / now do e x a c t same t h i n g s u s i n g o u t F i l e i n s t e a d o f cout / / ====================================================== / out F ile << f i x e d ; o u t F i l e. p r e c i s i o n (4); o u t F i l e. s e t f ( i o s b a s e : : showpoint ) ; o u t F i l e << Make and model : << automobile << endl ; o u t F i l e << Year : << y e a r << e n d l ; o u t F i l e << Was asking $ << a p r i c e << endl ; o u t F i l e << Now asking $ << d p r i c e << endl ; o u t F i l e. c l o s e ( ) ; // done with f i l e return 0 ; John Chrispell, Monday September 27, 2010 slide 9/17
Reading Text from a file To read text from a file you must: Include the fstream header file defining an ifstream class for handeling input. You declare an ifstream variable. Name it as you please. You must account for the std namespace. Associate an ifstream object with the file. We will use the open() method to do this and close() when we are done. Now you can use the << operator to read a variety of data types. You may also use the get() method to read individual characters and the getline() method for a line of characters at a time. You may use eof() and fail() to monitor the success of input attempts. Note the ifstream object when used as a test condition is a Boolean type. Ture when reads are successful. John Chrispell, Monday September 27, 2010 slide 11/17
sumafile.cpp #i n c lude <iostream > #i n c lude <fstream> // f i l e I /O suppport #i n c lude <c s t d l i b > // s u p p o r t f o r e x i t ( ) const i n t SIZE = 6 0 ; i n t main ( ) { using namespace std ; char f i l e n a m e [ SIZE ] ; ifstream infile ; // object for handling f i l e input cout << Enter name o f data f i l e : ; cin. getline ( filename, SIZE ) ; infile. open ( filename ) ; // associate infile with a f i l e / Test to see i f it opens / i f (! i n F i l e. i s o p e n ( ) ) { cout << Could not open the f i l e << filename << endl ; cout << Program t e r m i n a t i n g. \ n ; e x i t ( EXIT FAILURE ) ; double v a l u e ; // Place h o l d e r f o r f a l u e read. double sum = 0. 0 ; // Sum o f v a l u e s read i n t count = 0 ; // c o u n t e r f o r number o f i t e m s read / Read f i l e v a l u e s / i n F i l e >> v a l u e ; // get f i r s t v a l u e while ( infile. good ( ) ) { // while input good and not at EOF count++; // one more item read sum += value ; // c a l c u l a t e running t o t a l i n F i l e >> value ; // get next value John Chrispell, Monday September 27, 2010 slide 13/17
sumafile.cpp count / End t h i n g s i n some manner / i f ( i n F i l e. e o f ( ) ) { cout << End o f f i l e r e a c h e d. \ n ; e l s e i f ( i n F i l e. f a i l ( ) ) { cout << Input terminated by data mismatch. \ n ; e l s e { cout << Input terminated f o r unknown reason. \ n ; / Make a c o n c l u s i o n / i f ( count == 0){ cout << No data p r o c e s s e d. \ n ; e l s e { cout << Items read : << count << endl ; cout << Sum : << sum << endl ; cout << Average : << sum / count << e n d l ; i n F i l e. c l o s e ( ) ; // f i n i s h e d with the f i l e return 0 ; John Chrispell, Monday September 27, 2010 slide 15/17
Plain Pixel Map Files One particular image format is the plain pixel map or.ppm file. P3 10 10 255 255 255 255 255 255 John Chrispell, Monday September 27, 2010 slide 17/17