Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5.

Similar presentations


Presentation on theme: "CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5."— Presentation transcript:

1 CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5

2 3/11/02 HW 2 due in 2 weeks - start thinking about it –look at description of P6 for ideas –program exercises from book Program 4 –don't use Money class from book –printPrice should just print a price in the proper format - no more

3 8.1 Standard Input/Output Streams Stream is a sequence of characters Working with cin and cout Streams convert internal representations to character streams >> input operator (extractor) << output operator (inserter) Streams have no fixed size

4 8.2 External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete at start of day Interactive –Real time systems –Ok for smaller programs –Programs that complete quickly

5 Files How to attach files to the stream –stream object –external file name –internal name –open –test state –close

6 Files Declare the stream to be processed need to #include fstream ifstreamins;// input stream ofstream outs;// output stream Need to open the files ins.open (inFile); outs.open (outFile);

7 Files I/O just like using cin and cout fail() function - returns true nonzero if –file fails to open –input operation fails eof() - set to true when program tries to read beyond the end of the file –NOT before close() - disconnects the file from the stream variable

8 File Processing Loop processing –for loops –while loops Newline character –eof() function returns a False if file is not empty while ( ! ins.eof()) { do stuff }

9 Reading char and string Data with >> Leading white space skipped –newline also skipped do// pseudocode ch = next char in stream while ch is whitespace after loop, ch is the char that would be read by >> ch is first char of string read by >> –string ends when next space character is located

10 Other ways to read sometimes want to read white space characters –get function reads the next character –getline function reads up to the next newline which is thrown away instream.getline( char[], int) getline( instream, string) –can specify a different terminator for getline instream.getline( char[], int, char) getline( instream, string, char)

11 8.4 More on Reading String Data Getline - could be used to process an entire line of data Use # as a delimiter character getline (eds, name, ‘#’); Advance the newline getline (eds, name, ‘\n’); Use care when choosing cin, get or getline

12 Input/Output Manipulators setf, unsetf, precision and width Can be used with the cout and << #include iomanip when using Can be used with any output filestream

13 I/O Manipulators setiosflags(ios:: flagname)set the flag named flagname resetiosflags(ios:: flagname)reset the flag to the default state setw( n)set the field width to n setprecision( n)use n digits after decimal point

14 Formatting with State Flags Depending on the setiosflags or unsetiosflags –Output can be controlled by other format state flag –Flags are enumerated types ios::flagname –boolalpha, fixed, left, right, showpoint etc

15 ios flags left right justification in field fixedfixed point format scientificscientific notation

16 8.6 Common Programming Errors Connecting streams and external files –Declare stream object and open the file Watch use of while loops when processing –Test values see what you actually have Reading past the eof White space Newline character Formatting via flags

17 numbers strings C++ has stringstreams (sstream library) –use similar to iostreams –can get the corresponding string C has functions in stdlib.h –atoi and atof to convert appropriate strings to numbers –sscanf works like scanf but uses a char[] –sprintf is like printf but result is char[]


Download ppt "CS 117 Spring 2002 Using Files Hanly: Chapter 9, some in Chapter 4 Freidman-Koffman: Chapter 8, iomanip in Chapter 5."

Similar presentations


Ads by Google