Title: Chapter 12 File Input and Output
1Chapter 12File Input and Output
- 12.1 Text Output
- 12.2 Text Input
- 12.3 fstream and File Open Modes
- 12.4 Binary I/O
212.1 Text Output
- Writing Data to a File
- Using ofstream class
TextFileOutput
Run
3Notes
- Close file
- close() must be called to close the stream
- Otherwise, the data may not be saved properly
- File exists?
- The existing content in the file will be
destroyed without warning
4Path of file
- Absolute filename
- Complete path with drive letter.
- c\\example\\scores.txt
- Relative filename
- Path relative to the current location of the
program - scores.txt or ..\\scores.text
output.open("c\\example\\scores.txt")
5Formatting Output
- Using the same stream manipulators to format
output to the console
WriteFormatData
Run
612.2 Text Input
- Using the ifstream class
- ifstream input
- ifstream in
- ifstream a
- Using extraction operator gtgt
- For example
- Reads data from the file scores.txt, the file
created in the preceding example - inputgtgtfirstNamegtgtmigtgtlastNamegtgtscore
TextFileInput
Run
7Notes
- Knowing data format
- To read correctly
- Testing file existence
- If the file does not exist, your reading will
produce incorrect results - Using fail() after open() to check it
- Reurn true if file does not exist
- input.open(scores.txt)
- if(input.fail())
- Testing end of file
- Using the eof() function
TestEndOfFile
Run
8Input Using getline
- gtgt is suitable for numerical values not strings
- Data are delimited by whitespace
- You can use getline() to read strings from a
file - getline(char array, int size, char delimitChar)
ReadCity
Run
9get and put
- get
- On an input object to read a character
- put
- On an output object to write a character
CopyFile
Run
1012.3 fstream and File Open Modes
- fstream
- Combination of ifstream and ofstream
- For input and/or output
- A file mode must be specified for fstream
- To tell C how the file will be used
11File Open Mode List (p382)
12Combining Modes
- Using the operator
- bitwise inclusive OR operator
AppendFile
inout.open(iosin iosout iosapp)
inout.open("city.txt", iosout iosapp)
Run
Resulting AND in open mode!
0 1 0
in out app
13Testing Stream States
- State bits
- Each stream object contains a set of bits that
act as flags. - These bit values (0 or 1) indicate the state of a
stream. - List of State Bits (P383)
14Stream State Functions
ShowStreamState
Run
1512.4 Binary I/O
- The binary mode iosbinary is necessary
- By default, a file is opened in text mode.
- Binary file vs. text file
- a sequence of characters
- a sequence of bits
199
0000 0000 1100 0111
All files are stored in binary format, and thus
all files are essentially binary files.
Text I/O is built upon binary I/O to provide a
level of abstraction for character encoding and
decoding.
16The write Function
The syntax for the write function is
streamobject.write(char address, int
size) Listing 12.9 shows an example of using the
write function.
BinaryCharOutput
Run
17Write Any Type
- Numerical values cannot be written directly
- Use reinterpret_cast before writing
- reinterpret_castltdataTypegt(address)
int value 199 out.wirte(reinterpret_castltchar
gt(value), sizeof(value))
0000 0000 1100 0111
\0 \0 \12 \7
199
BinaryIntOutput
Run
sizeof(value)?
18The read Function
The syntax for the read function is
streamobject.read(char address, int
size) Assume the file city.dat was created in
Listing 12.9. Listing 12.11 reads the characters
using the read function.
BinaryCharInput
Run
19Read Any Type
Also, use the reinterpret_cast operator
int value 199 out.wirte(reinterpret_castltchar
gt(value), sizeof(value))
int value2 out.read(reinterpret_castltchar
gt(value2), sizeof(value2))
BinaryIntInput
Run
20Binary Array I/O
- Listing 12.13
- To write an array of double values to a binary
file and read them back - double arraySIZE 3.4, 1.3, 2.5, 5.66, 6.9
- binaryio.write(reinterpret_castltchar gt(array),
sizeof(array))
sizeof(array)?
BinaryArrayIO
Run
21Binary Object I/O
- Write/Read object data to/from a binary file
Student student1("John", 'T', "Smith", 90)
Student student2("Eric", 'K', "Jones", 85)
binaryio.write(reinterpret_castltchar gt
(student1), sizeof(Student))
binaryio.write(reinterpret_castltchar gt
(student2), sizeof(Student))
Student.h
Run
BinaryObjectIO
Student.cpp
22Random Access File
- A file consists of a sequence of bytes
- File pointer
- A special marker for the location of the read or
write operation - When a file is opened, the file pointer is set at
the beginning of the file - When you read or write data to the file, the file
pointer moves forward to the next data item
23Random Access File
Two pointers One for read(input) and the other
for write(output)
24Random Access File
- To read/write at some specific location?
25Random Access File Example
Listing 12.17 demonstrates how to access file
randomly. The program first stores 10 student
objects into the file Then it retrieves the 3rd
student from the file.
Run
RandomAccessFile
26Updating Files
- Listing 12.18 demonstrates how to update a file.
- Suppose file object1.dat has already been created
with ten Student objects from Listing 10.17. - The program
- reads the 3rd student from the file,
- changes the last name,
- writes the revised object back to the file, and
- reads the new object back from the file.
Run
UpdateFile
27Summary
- ifstream ofstream fstream
- Open mode
- read() write() get() put() getline()
- reinterpret_cast
- seekg() seekp()