Presentation is loading. Please wait.

Presentation is loading. Please wait.

16/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files All the data used in our programs so far has been entered through the keyboard and.

Similar presentations


Presentation on theme: "16/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files All the data used in our programs so far has been entered through the keyboard and."— Presentation transcript:

1 16/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files All the data used in our programs so far has been entered through the keyboard and displayed to the screen. Limiting: Sometimes there is much data to be processed. Sometimes there is much data to be processed. Sometimes we want a permanent record of the data. Sometimes we want a permanent record of the data.

2 26/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Files One type of file is a data file: a file which is created/updated and/or input by a program. Two Types: Random Access: the program can input any record from the file by specifying a record number. We will not be studying these – they are somewhat complicated. Random Access: the program can input any record from the file by specifying a record number. We will not be studying these – they are somewhat complicated. Sequential: the program may only write or read (not both) adjacent data from the beginning of the file. Sequential: the program may only write or read (not both) adjacent data from the beginning of the file.

3 36/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Sequential Disk Files AKA text files or ASCII files (and other names). They can be created with many different software packages. Notepad will create text files. Disk files have: A directory. A directory. A file name. A file name.

4 46/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Streams A sequential disk file is treated as a stream. A stream: Is a sequence of characters. Is a sequence of characters. Has whitespace embedded in it. Has whitespace embedded in it. Blanks, tabs, newlines. Whitespace is used to control input operations.

5 56/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Steps to use a data file #include to access library. Create an object of type ifstream or ofstream. A file will be used for input only or output only. A file will be used for input only or output only. This object is used to control I/O (file-control block). This object is used to control I/O (file-control block). Open the file. Do inputs or outputs to the file. Close the file.

6 66/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Opening/Closing Data Files.open( ); or.open( );.open( ); or.open( ); Append:.open(, ios::app); Examples: timecards.open(“week080308.txt”); timecards.open(“week080308.txt”); report.open(“todays_sales.txt”); report.open(“todays_sales.txt”); weights.open(exper_filename); weights.open(exper_filename); When all done doing I/O, close the file..close(); or.close();.close(); or.close();

7 76/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Reading Use the extractor operator (>>): weights >> w; weights >> w; employees >> name; employees >> name; Use the getline function: getline(timecards,emp_data); getline(timecards,emp_data); getline(weights,bridge_data,’\n’); getline(weights,bridge_data,’\n’); Use the get function for character-at-a-time input: inf.get(c); inf.get(c); Use the eof function to test whather eof has been reached – it is set when the last data item is read. while (!timecards.eof()) { … } while (!timecards.eof()) { … }

8 86/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Echoing Inputs Because the input is not typed on the screen, it is useful to the user if data file input is echoed to the screen or output file. This way the user knows what the input is and knows it was read correctly.

9 96/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Writing Use the insertion operator (<<): report << emp_line; report << emp_line; results << new_weight; results << new_weight; Use the put function for character at a time output: outf.put(c); outf.put(c);

10 106/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files I/O Manipulators Functions in the libraries iostream or iomanip. Control the format of output (or sometimes input). setw(int n): set the width of the next output value. setw(int n): set the width of the next output value. setprecision(int n): set the precision for output. setprecision(int n): set the precision for output. showpoint: causes a decimal point to always be displayed. showpoint: causes a decimal point to always be displayed. Left, right: cause output to be left or right justified in output field. Left, right: cause output to be left or right justified in output field. See table 8.3 on page 415.

11 116/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files I/O Errors.fail() or.fail() will return true if the last I/O operation failed..fail() or.fail() will return true if the last I/O operation failed. Why might an I/O operation fail? File does not exist for opening. File does not exist for opening. Reading past end-of-file. Reading past end-of-file. Writing to a file when the disk is full. Writing to a file when the disk is full. Others. Others. Typically follow any I/O operation with: if.fail() { }

12 126/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Cin/Cout The keyboard is just handled as a pre- defined and opened input stream called cin. The screen is just handled as a pre- defined and opened output stream called cout.

13 Files versus Humans When input is coming from the keyboard through cin, if there is an error in the input, the program can identify it and correct it by re-prompting the user (interactive). But when the input is coming from a file, there generally is not any method for correcting errors in input and continuing – all the program can do is report the error and terminate gracefully without doing any more processing. 136/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files

14 146/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files Reading Chapter 8 covers these same concepts. Reviewing them may help clarify the material.

15 Batch Processing What: a program that receives all of its input, then does all of its processing and output to permanent storage files (versus interactive programs). Why: Programs that process files may take a relatively long time to complete, depending on the size of the input, the amount of processing, and the output. 156/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files

16 Batch Processing Main function command line arguments and return value: int main(int argc, char *argv[]) Enables a program to be executed from the command line with no user intervention cerr: similar to cout, but used for error messages exit(int error_code): immediately terminates the program, returning error_code EXIT_SUCCESS, EXIT_FAILURE: predefined constants that may be used to signal exit status. 166/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files

17 Batch Processing I/O Redirection The input from the keyboard and the output to the screen may be redirected from the command line. The input from the keyboard and the output to the screen may be redirected from the command line. This allows a program to be executed in the background or its own window if it does use cin or cout. This allows a program to be executed in the background or its own window if it does use cin or cout. This also allows for piping: the output of one program becomes the input of another program. UNIX does this using filters: reusable tools. EG: sort, grep, awk, sed, more, head, tail. This also allows for piping: the output of one program becomes the input of another program. UNIX does this using filters: reusable tools. EG: sort, grep, awk, sed, more, head, tail. 176/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files

18 Batch Processing This material on batch processing is useful for working in production environments or for working in a command line oriented operating system like UNIX. Data can be cut-and-pasted into or out of a console window, which works well for small amounts of data. 186/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files


Download ppt "16/24/2015 1:00 AM6/24/2015 1:00 AM6/24/2015 1:00 AMStreams and Files All the data used in our programs so far has been entered through the keyboard and."

Similar presentations


Ads by Google