Top Banner
1 03/21/22 03/21/22 16:28 16:28 Streams and Files Streams and Files Streams and Files Streams and Files All the data used in our All the data used in our programs so far has been programs so far has been entered through the keyboard entered through the keyboard and displayed to the screen. and displayed to the screen. Limiting: Limiting: Sometimes there is much data to be Sometimes there is much data to be processed. processed. Sometimes we want a permanent Sometimes we want a permanent record of the data. record of the data.
18

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.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 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.

1104/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

Streams and FilesStreams and Files

All the data used in our programs so far All the data used in our programs so far has been entered through the keyboard has been entered through the keyboard and displayed to the screen.and displayed to the screen.

Limiting:Limiting: Sometimes there is much data to be Sometimes there is much data to be

processed.processed. Sometimes we want a permanent record of Sometimes we want a permanent record of

the data.the data.

Page 2: 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.

2204/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

FilesFiles

One type of file is a data file: a file which is One type of file is a data file: a file which is created/updated and/or input by a program.created/updated and/or input by a program.

Two Types:Two Types: Random Access: the program can input any record Random Access: the program can input any record

from the file by specifying a record number. We will from the file by specifying a record number. We will not be studying these – they are somewhat not be studying these – they are somewhat complicated.complicated.

Sequential: the program may only write or read (not Sequential: the program may only write or read (not both) adjacent data from the beginning of the file.both) adjacent data from the beginning of the file.

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

3304/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

Sequential Disk FilesSequential Disk Files

AKA text files or ASCII files (and other AKA text files or ASCII files (and other names).names).

They can be created with many different They can be created with many different software packages.software packages.

Notepad will create text files.Notepad will create text files.

Disk files have:Disk files have: A directory.A directory. A file name.A file name.

Page 4: 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.

4404/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

StreamsStreams

A sequential disk file is treated as a A sequential disk file is treated as a stream.stream.

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.Blanks, tabs, newlines.

Whitespace is used to control input operations.Whitespace is used to control input operations.

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

5504/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

Steps to use a data fileSteps to use a data file

#include <fstream> to access library.#include <fstream> to access library.Create an object of type ifstream or Create an object of type ifstream or ofstream.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 This object is used to control I/O (file-control

block).block).

Open the file.Open the file.Do inputs or outputs to the file.Do inputs or outputs to the file.Close the file.Close the file.

Page 6: 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.

6604/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

Opening/Closing Data FilesOpening/Closing Data Files

<ifstream>.open(<filename>); or <ifstream>.open(<filename>); or <ofstream>.open(<filename>);<ofstream>.open(<filename>);

Append: <ofstream>.open(<filename>, ios::app);Append: <ofstream>.open(<filename>, ios::app);

Examples: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.When all done doing I/O, close the file. <ifstream>.close(); or <ofstream>.close();<ifstream>.close(); or <ofstream>.close();

Page 7: 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.

7704/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

ReadingReading

Use the extractor operator (>>):Use the extractor operator (>>): weights >> w;weights >> w; employees >> name;employees >> name;

Use the getline function: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: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 Use the eof function to test whather eof has been reached – it is set when the last data item is read.reached – it is set when the last data item is read.

while (!timecards.eof()) { … }while (!timecards.eof()) { … }

Page 8: 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.

8804/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

Echoing InputsEchoing Inputs

Because the input is not typed on the Because the input is not typed on the screen, it is useful to the user if data file screen, it is useful to the user if data file input is echoed to the screen or output file. input is echoed to the screen or output file. This way the user knows what the input is This way the user knows what the input is and knows it was read correctly.and knows it was read correctly.

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

9904/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

WritingWriting

Use the insertion operator (<<):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 Use the put function for character at a time output:output: outf.put(c);outf.put(c);

Page 10: 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.

101004/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

I/O ManipulatorsI/O Manipulators

Functions in the libraries iostream or iomanip.Functions in the libraries iostream or iomanip.

Control the format of output (or sometimes input).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 showpoint: causes a decimal point to always be

displayed.displayed. Left, right: cause output to be left or right justified in Left, right: cause output to be left or right justified in

output field.output field.

See table 8.3 on page 415.See table 8.3 on page 415.

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

111104/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

I/O ErrorsI/O Errors

<ifstream>.fail() or <ofstream>.fail() will <ifstream>.fail() or <ofstream>.fail() will return true if the last I/O operation failed.return true if the last I/O operation failed.Why might an I/O operation fail?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 < Typically follow any I/O operation with: if < … >.fail() { <display an error message> }… >.fail() { <display an error message> }

Page 12: 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.

121204/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

Cin/CoutCin/Cout

The keyboard is just handled as a pre-The keyboard is just handled as a pre-defined and opened input stream called defined and opened input stream called cin.cin.

The screen is just handled as a pre-The screen is just handled as a pre-defined and opened output stream called defined and opened output stream called cout.cout.

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

Files versus HumansFiles versus Humans

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

131304/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

Page 14: 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.

141404/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

ReadingReading

Chapter 8 covers these same concepts. Chapter 8 covers these same concepts. Reviewing them may help clarify the Reviewing them may help clarify the material.material.

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

Batch ProcessingBatch Processing

What: a program that receives all of its What: a program that receives all of its input, then does all of its processing and input, then does all of its processing and output to permanent storage files (versus output to permanent storage files (versus interactive programs).interactive programs).

Why: Programs that process files may Why: Programs that process files may take a relatively long time to complete, take a relatively long time to complete, depending on the size of the input, the depending on the size of the input, the amount of processing, and the output.amount of processing, and the output.

151504/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

Page 16: 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.

Batch ProcessingBatch Processing

Main function command line arguments Main function command line arguments and return value:and return value:

int main(int argc, char *argv[])int main(int argc, char *argv[])

Enables a program to be executed from the Enables a program to be executed from the command line with no user interventioncommand line with no user intervention

cerr: similar to cout, but used for error messagescerr: similar to cout, but used for error messages

exit(int error_code): immediately terminates the exit(int error_code): immediately terminates the program, returning error_codeprogram, returning error_code

EXIT_SUCCESS, EXIT_FAILURE: predefined EXIT_SUCCESS, EXIT_FAILURE: predefined constants that may be used to signal exit status.constants that may be used to signal exit status.

161604/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

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

Batch ProcessingBatch Processing

I/O RedirectionI/O Redirection The input from the keyboard and the output to the The input from the keyboard and the output to the

screen may be redirected from the command line.screen may be redirected from the command line. This allows a program to be executed in the This allows a program to be executed in the

background or its own window if it does use cin or background or its own window if it does use cin or cout.cout.

This also allows for piping: the output of one This also allows for piping: the output of one program becomes the input of another program. program becomes the input of another program. UNIX does this using filters: reusable tools. EG: UNIX does this using filters: reusable tools. EG: sort, grep, awk, sed, more, head, tail.sort, grep, awk, sed, more, head, tail.

171704/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files

Page 18: 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.

Batch ProcessingBatch Processing

This material on batch processing is useful This material on batch processing is useful for working in production environments or for working in production environments or for working in a command line oriented for working in a command line oriented operating system like UNIX.operating system like UNIX.

Data can be cut-and-pasted into or out of Data can be cut-and-pasted into or out of a console window, which works well for a console window, which works well for small amounts of data.small amounts of data.

181804/18/2304/18/23 21:3621:36 Streams and FilesStreams and Files