1 / 18

Streams and Files

Streams 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 we want a permanent record of the data. Files.

sileas
Download Presentation

Streams and Files

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. Streams 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 we want a permanent record of the data. Streams and Files

  2. 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. • Sequential: the program may only write or read (not both) adjacent data from the beginning of the file. Streams and Files

  3. 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 file name. Streams and Files

  4. Streams • A sequential disk file is treated as a stream. • A stream: • Is a sequence of characters. • Has whitespace embedded in it. • Blanks, tabs, newlines. • Whitespace is used to control input operations. Streams and Files

  5. Steps to use a data file • #include <fstream> to access library. • Create an object of type ifstream or ofstream. • A file will be used for input only or output only. • This object is used to control I/O (file-control block). • Open the file. • Do inputs or outputs to the file. • Close the file. Streams and Files

  6. Opening/Closing Data Files • <ifstream>.open(<filename>); or <ofstream>.open(<filename>); • Append: <ofstream>.open(<filename>, ios::app); • Examples: • timecards.open(“week080308.txt”); • report.open(“todays_sales.txt”); • weights.open(exper_filename); • When all done doing I/O, close the file. • <ifstream>.close(); or <ofstream>.close(); Streams and Files

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

  8. 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. Streams and Files

  9. Writing • Use the insertion operator (<<): • report << emp_line; • results << new_weight; • Use the put function for character at a time output: • outf.put(c); Streams and Files

  10. 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. • setprecision(int n): set the precision for output. • showpoint: causes a decimal point to always be displayed. • Left, right: cause output to be left or right justified in output field. • See table 8.3 on page 415. Streams and Files

  11. I/O Errors • <ifstream>.fail() or <ofstream>.fail() will return true if the last I/O operation failed. • Why might an I/O operation fail? • File does not exist for opening. • Reading past end-of-file. • Writing to a file when the disk is full. • Others. • Typically follow any I/O operation with: if < … >.fail() { <display an error message> } Streams and Files

  12. 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. Streams and Files

  13. Files versus Humans Streams and Files 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.

  14. Reading • Chapter 8 covers these same concepts. Reviewing them may help clarify the material. Streams and Files

  15. Batch Processing Streams and Files 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.

  16. Batch Processing Streams and Files • Main function command line arguments and return value: • int main(intargc, 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(interror_code): immediately terminates the program, returning error_code • EXIT_SUCCESS, EXIT_FAILURE: predefined constants that may be used to signal exit status.

  17. Batch Processing Streams and Files • I/O Redirection • 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 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.

  18. Batch Processing Streams and Files 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.

More Related