input output file
TRANSCRIPT
-
8/14/2019 Input Output File
1/27
1
Input Output File
LECTURE
-
8/14/2019 Input Output File
2/27
2
Objectives
One dimensional array, two dimensionalarray, multidimensional array
Declaration, Assignment, Initialization,Operation on array
String of Characters
2
-
8/14/2019 Input Output File
3/27
3
Objectives
Open a sequential access file
Determine whether a file was openedsuccessfully
Write data to a sequential access file
Read data from a sequential access file
3
-
8/14/2019 Input Output File
4/27
4
Objectives (continued)
Test for the end of a sequential accessfile
Close a sequential access file
Read information from and writeinformation to a sequential access filein .NET C++
4
-
8/14/2019 Input Output File
5/27
5
Concept Lesson
File Types
Using Sequential Access Files
Creating and Opening a SequentialAccess File
Determining whether a File was OpenedSuccessfully
5
-
8/14/2019 Input Output File
6/27
6
Concept Lesson (continued)
Writing Information to a SequentialAccess File
Reading Information from a Sequential
Access FileTesting for the End of a Sequential
Access File
Closing a Sequential Access File
6
-
8/14/2019 Input Output File
7/27
7
File Types
A program can read from or write to a file
Files to which information is written are output filesFiles that are read by the computer are input files
Types of files in C++
Sequential Information is accessed in consecutive order
Random
Can be accessed in consecutive or in random order
Binary
Information can be accessed by its byte location
7
-
8/14/2019 Input Output File
8/27
8
Using Sequential AccessFiles A sequential access file is often called
a text file
8
-
8/14/2019 Input Output File
9/27
9
Using Sequential Access Files(continued)
9
-
8/14/2019 Input Output File
10/27
10
Creating and Opening aSequential Access File
You must create the input and output fileobjects used in a program #include
ifstream and ofstream classesusing std::ifstream; and usingstd::ios;
10
-
8/14/2019 Input Output File
11/27
Sequential Access File(continued)
11
11
-
8/14/2019 Input Output File
12/27
12
Sequential Access File(continued)12
-
8/14/2019 Input Output File
13/27
13
Sequential Access File(continued)13
-
8/14/2019 Input Output File
14/27
14
Determining whether a Filewas Opened Successfully
open() may fail when attempting toopen a file
E.g., it will not be able to create an outputfile when the path in fileName does notexist, or when the disk is full
14
-
8/14/2019 Input Output File
15/27
was Opened Successfully(continued)
15
! is the Not logical operator
15
-
8/14/2019 Input Output File
16/27
16
Sequential Access File(continued) Field: single item of information about a person,
place, or thing E.g., a name, a salary, a SSN, or a price
Record: a collection of one or more related fields
Contains data about a specific person, place, or thingThe college you are attending keeps student records
Examples of fields include your SSN, name, address, phonenumber, credits earned, and grades earned
16
-
8/14/2019 Input Output File
17/27
17
17
Writing Information to aSequential Access File
(continued)
-
8/14/2019 Input Output File
18/27
18
Writing Information to aSequential Access File
-
8/14/2019 Input Output File
19/27
19
Sequential Access File(continued)To verify if information was written
correctly, open the (sequential access)file in a text editor
E.g., the text editor in Visual C++ orNotepad
19
-
8/14/2019 Input Output File
20/27
20
Reading Information from aSequential Access File
Use >> to read char and numeric datafrom a file
Use getline() to read string data from
a sequential access fileThe default delimiter character is the
newline character (\n)
20
-
8/14/2019 Input Output File
21/27
21
21
Reading Information from aSequential Access File
(continued)
-
8/14/2019 Input Output File
22/27
22
Reading Information from aSequential Access File
(continued)
i f h d f
-
8/14/2019 Input Output File
23/27
23
Testing for the End of aSequential Access File
A file pointer keeps track of the nextcharacter either to read from or write toa file
When a sequential access file is opened forinput, the file pointer is positioned beforethe first character
As characters are read, the pointer is
moved forward
23
-
8/14/2019 Input Output File
24/27
Sequential Access File(continued)
24
24
l i i l
-
8/14/2019 Input Output File
25/27
25
Closing a Sequential AccessFileTo prevent the loss of data, close a
sequential access file as soon asprogram finishes using it
25
-
8/14/2019 Input Output File
26/27
26
Summary
Sequential access files can be input or output files
To use a text file, program must contain: include directive
using std::ios; statement Use the ifstream and ofstream classes to create
input and output file objects, respectively
Use is_open() to determine whether a text file
was opened successfully
26
-
8/14/2019 Input Output File
27/27
27
Summary (continued)
Records in a text file are usually writtenon a separate line in the fileUse endl
eof() determines if file pointer is at endof the file
Use close() to close a file
Failing to close an open file can result inloss of data
27