grade.cpp, data
and
bad_data
.
ifstream
, and
those corresponding to an output file are of class ofstream
:
ifstream inFile; ofstream outFile;A file is opened using a method
open(filename,mode)
, where
filename
is the name of the file (in quotation marks), and
mode
is the mode in which a file is opened, s.a. reading,
writing, appending, etc. For instance, the code
inFile.open("myfile",ios::in); outFile.open("myfile2", ios::out);opens a file
myfile
for reading, and myfile2
for
writing. After the files have been opened, their variables (in this case
inFile
and outFile
) can be used in the rest of
the program the same way as cin
and cout
.
These variables are sometimes called file handles.
In formatted input and output
operator >>
automatically figures out the type of variables
that the data is being stored in. Example:
int a, b; inFile >> a >> b;Here the input is expected to be two integers. If the input is anything other than two integers (say, an integer and a float), the input operator returns value 'false'. This allows to check if the data entered by the user is of the correct type, as follows:
if (inFile >> a >> b) outFile << a << b << endl; else cerr << "Invalid data" << endl;Similarly we can place input operator into the condition of a loop. Since operator
>>
returns 'false' at the end of file as well, in
the case when all the data is valid the loop will stop at the end of file.
All files opened in the program must be closed in the end of the
program. To close a file, use method close
:
inFile.close();Closing an output file causes all the data from the output buffer to be actually written to the file.
data
contains a list of names and scores in the
following format:
john 85 mary 88 jake 99The assignment for this part of the lab is to write a program that reads such data from a file
data
, increase every score by 10,
and write the output to a new file result
. The output should
look as follows:
john 95 mary 98 jake 109Exercise 1. Fill in the missing code in the file
grade.cpp
. Compile and run your program. Check
the
results in the file result
to make sure that the program
works correctly.
Question 1.
Change your program so that the input is taken from the file
bad_data
rather than data
. What gets written to
the file result
? Why?
Question 2. Give another example of a file with invalid input. What is the output for this file?
modify.cpp
. Also download files
data
and
bad_data
if you have not done so already.
get()
(several versions), getline()
, and
read()
. Methods for unformatted output are put()
and write()
.
The simplest case of unformatted input/output is reading or writing a file character-by-character (recall that the size of a character is exactly one byte). This is a convenient way of processing text files. Example:
char c; inFile.get(c); outFile.put(c);The first command reads a character from an input file associated with
inFile
and stores this character in c
. The
second writes character c
to an output file associated with
outFile
.
In the case of unformatted input there is no such thing as invalid data,
since any
sequential file can be read byte-by-byte. However, the method call to
get()
returns 'false' when the end of the file is
reached. Placing
the input statement in the condition of the while loop (as we did for the
formatted input) will cause the loop to stop at the end of the file.
Opening and closing of input/output files for unformatted data is the same as for formatted input/output.
islower()
, to change it to upper
case use a function toupper()
.
Exercise 2.
Fill in the missing code in the file modify.cpp
. Compile and run the program. Check
the results in the file result2
to make sure that the program
works correctly.
Question 3. Which line of code makes the program stop at the end of the file?
Question 4.
Change the program so that the input is taken from the file
bad_data
. What is the output in this case?