0% found this document useful (0 votes)
9 views

Lab 3 4 Using Files for Input and Output

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab 3 4 Using Files for Input and Output

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 3.

4 Using Files for Input and Output

Data can be entered for processing by a computer program from the standard input device,
the keyboard, and output to the standard output device, the screen or monitor. Using the
keyboard for entering large amounts of data to be processed is not efficient. Instead, data
can be read from files on storage media as program input. Likewise, outputting substantial
amounts of information to the screen is not very practical. As an alternative, program
output can be directed to files on secondary storage devices.

The C++ syntax for inputting and outputting data using files is similar to that for the input
and output to the standard devices. File I/O requires five steps:

1. Standard device I/O requires the preprocessor directive #include <iostream>.


File I/O necessitates using the preprocessor directive #include <fstream>.

Note that a program could get both standard and file input and output to both the
screen and a file. If such is the case, both preprocessor directives, iostream and
fstream, would be necessary.

2. Unlike standard I/O, stream variables must be declared for file I/O. The data type for
file input variables is ifstream, and the data type for file output variables is
ofstream.

3. In addition, the stream variables must be associated with an input or output file.
When an output file variable is associated with a physical file name, the file need not
be in existence, and, if it is not, it will be created by default. Input file variables must
be associated with a file name of a currently existing file. Both input and output files
are set to a file mode, indicating how they are to be used in the program. The file
mode can be included as a parameter in the open function. If no file mode is
included as a parameter, the default is input when the input stream variable opens
the input file and output when the output stream variable opens the output file. In
output mode, any data written to the output file will overwrite any data that is in
the file. An append file mode parameter for an output file will allow data currently
in the file to remain and will add or append new output to the end of the currently
existing data.

4. Like standard, the I/O file stream variables are used with the extraction operator >>
and the insertion operator <<, as well as with other file functions such as get,
getline, ignore, putback, and peek.

5. The stream variables should be closed with the close function.


Objectives
In this lab, you learn to use files for input and output.
After completing this lab, you will be able to:
• Associate file stream variables with physical files.
• Open and close input and output files.
• Read data from input files.
• Write data to output files.

Estimated completion time: 50–60 minutes


Lab 3.4 Steps: Using Files for Input and Output
In the following exercises, you are to design and write programs that will use files for input
and/or output.

1a. Critical Thinking Exercise: Design a program that will that will accept the date and the
high and low temperatures (in Fahrenheit) for that date. The program should average
those temperatures to report the daily average temperature and then output the date
and the average temperature to a file.

Use a string data type for the date and enter the date in the mm/dd/yy format. Use a
double data type for the high, low, and average temperatures. The program should
output the date and the average temperature to a file called temps.txt. Separate the
date from the average with a space in the output. The output file should be in the same
directory as the source code file. The output file will contain data only. Do not include
column headings.

Write your design in the following space. For your design, write an algorithm (an English
language description of the steps required to successfully write the program).

1b. Write the source code for the design you created in Exercise 1a and name it
fahrenheit.cpp. Use the following data sets to test your program.

Input Output
date high low date average
01/01/13 38 18 01/01/13 28
01/02/13 44 25 01/02/13 34.5
1c. Enter, compile, link, and run fahrenheit.cpp. Save fahrenheit.cpp and the output file
temps.txt in the Chap03 folder of your Student Data Files.

Print the output file and submit it with your work. You can open your files within your
SDK. (Hint: In the Open dialog box, remember to change the “Look in” box to the proper
drive and the “Files of type” box to All Files.)

2a. Design a program that will get the input from the output file temps.txt that you created
in Exercise 1. The program will read the date and average from the temps.txt file. Again,
use a string data type for the date and a double for the average. The program
should then convert the average (Fahrenheit) to Celsius. The conversion formula is
subtract 32 from the average (Fahrenheit) and then multiply by five and divide by nine,
e.g., (average – 32) * 5 / 9. Output the date and the Celsius temperature to a file named
celsius.txt and save it in the Chap03 folder of your Student Data Files. Use the
manipulators you have learned about in this chapter to format the output to one
decimal space.

Write your design in the following space. For your design, write an algorithm (an English
language description of the steps required to successfully write the program).

2b. Write a C++ program based on the design you created in Exercise 2a and name it
conversion.cpp.

2c. Enter, compile, link, and run conversion.cpp. Save Conversion.cpp and the output file
celsius.txt in the Chap03 folder of your Student Data Files.

You might also like