ifstream, and those corresponding to an output file are of class
ifstream inFile; ofstream outFile;A file is opened using a method
filenameis the name of the file (in quotation marks), and
modeis 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
myfilefor reading, and
myfile2for writing. After the files have been opened, their variables (in this case
outFile) can be used in the rest of the program the same way as
cout. These variables are sometimes called file handles.
In formatted input and output
>> 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
inFile.close();Closing an output file causes all the data from the output buffer to be actually written to the file.
datacontains 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
resultto make sure that the program works correctly.
Change your program so that the input is taken from the file
bad_data rather than
data. What gets written to
Question 2. Give another example of a file with invalid input. What is the output for this file?
modify.cpp. Also download files
bad_dataif you have not done so already.
read(). Methods for unformatted output are
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
inFileand stores this character in
c. The second writes character
cto an output file associated with
In the case of unformatted input there is no such thing as invalid data,
sequential file can be read byte-by-byte. However, the method call to
get() returns 'false' when the end of the file is
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
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
Question 3. Which line of code makes the program stop at the end of the file?
Change the program so that the input is taken from the file
bad_data. What is the output in this case?