Module 6 Files

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

The ________header file defines the data types ofstream, ifstream, and fstream.

<fstream>

ios::in

Input mode. Data will be read from the file. If the file does not exist, it will not be created, and the open function will fail.

his statement opens the file info.txt in both input and output modes. This means data may be written to and read from the file.

dataFile.open("info.txt", ios::in | ios::out);

the >> operator not only reads data from a file, but also returns a -------------- value indicating whether the data was successfully read or not. If the operator returns true, then a value was successfully read. If the operator returns false, it means that no value was read from the file.

true or false

Before data can be written to or read from a file, the following things must happen:

1- A file stream object must be created. 2- The file must be opened and linked to the file stream object.

two reasons a program should close files when it is finished using them: ____________________

1- Closing a file causes any unsaved data that may still be held in a buffer to be saved to its file. This means the data will be in the file if you need to read it later in the same program. 2 - it will not deplete more of the operating system's resources than necessary.

The file _________ contains all the declarations necessary for file operations.

<fstream>

ios::app

Append mode. If the file already exists, its contents are preserved and all output is written to the end of the file. By default, this flag causes the file to be created if it does not exist

ios::binary

Binary mode. When a file is opened in binary mode, data are written to or read from it in pure binary format. (The default mode is text.)

There is a way to determine whether the open member function successfully opened the file. After you call the open member function, you can test the file stream object as if it were a _____________

Boolean expression.

fstream

File stream. Objects of this data type can be used to open files for reading, writing, or both.

ios::trunc

If the file already exists, its contents will be deleted (truncated). This is the default mode used by ios::out.

ios::ate

If the file already exists, the program goes directly to the end of it. Output may be written anywhere in the file.

ifstream

Input file stream. You create an object of this data type when you want to open an existing file and read data from it.

When a file is used by a program, three steps must be taken.

Open the file, Process the file, Close the file

ofstream

Output file stream. You create an object of this data type when you want to create a file and write data to it.

ios::out

Output mode. Data will be written to the file. By default, the file's contents will be deleted if it already exists.

When a piece of data is read from a file, it is copied from the file into a variable in ______.

RAM

You cannot change the fact that ifstream files may only be read from, and ofstream files may only be written to. You can, however, vary the way operations are carried out on these files by providing a file ---------- as an optional second argument to the open function.

access flag

When a file is opened in ________, its contents are preserved, and all subsequent output is appended to the file's end.

append mode

A __________ contains data that has not been converted to text. Thus, you cannot view the contents of a binary file with a text editor.

binary file

Opening a file creates a _________ between the file and the program.

connection

you can use the >> operator to read data such as this from a text file into a numeric variable, and the >> operator will automatically ______the data to a numeric data type.

convert

he following statement uses the ios::in access flag to open a file in input mode, which allows data to be read from the file.

dataFile.open("info.txt", ios::in);

The following statement opens the file in such a way that data will only be written to its end:

dataFile.open("info.txt", ios::out | ios::app);

As with ifstream and ofstream objects, you use an fstream object's open function to open a file. An fstream object's open function requires two arguments, however. The first argument is a string containing the name of the file. The second argument is a file access flag that indicates the mode in which you wish to open the file.

dataFile.open("info.txt", ios::out);

After the program is finished using the file, the file must be closed. Closing a file _________ the file from the program.

disconnects

When a piece of data is written to a file, it is copied from a variable in RAM to the _______

file

A ____________ is an object that is associated with a specific file and provides a way for the program to work with that file. It is called a "stream" object because a file can be thought of as a stream of data.

file stream object

Files on a disk are identified by a ______

filename.

By using different combinations of access______, you can open files in many possible modes.

flags

This statement defines an fstream object named dataFile and uses it to open the file names.txt. The file is opened in both input and output modes. This technique eliminates the need to call the open function when your program knows the name and access mode of the file at the time the object is defined.

fstream dataFile("names.txt", ios::in | ios::out);

You define an fstream object just as you define objects of other data types. The following statement defines an fstream object named dataFile: ____

fstream dataFile;

Process the file—Data is either written to the file __________or read from the file if it is an input file .

if it is an output file

_______The file is opened for input only. Data may be read from the file, but not written to it. The file's contents will be read from its beginning. If the file does not exist, the open function fails.

ifstream

It is possible to define a file stream object and open a file in one statement.

ifstream inputFile("Customers.txt");

code shows an example of opening a file for input (reading).

ifstream inputFile; inputFile.open("Customers.txt");

An _______ file is a file from which data is read. It is called an input file because the program gets input from the file.

input

The >> operator reads not only user input from the cin object, but also data from a file. Assuming input File is an if stream object, the following statement shows the >> operator reading data from the file into the variable name:

inputFile >> name;

Calling the file stream object's close member function closes a file.

inputFile.close();

When used with the -------, however, the file's existing contents are preserved. If the file does not already exist, it will be created.

ios::in flag

When used by itself, the ----- causes the file's contents to be deleted if the file already exists.

ios::out flag

When a program uses a file to write or read a large amount of data, a -------- is typically involved.

loop

Remember when data is stored in a text file, it is encoded as text, using a scheme such as ASCII or Unicode. Even if the file contains --------, those --------- are stored in the file as a series of characters.

numbers, numbers

________ the file is opened for output only. Data may be written to the file, but not read from the file. If the file does not exist, it is created. If the file already exists, its contents are deleted (the file is truncated).

ofstream

code shows an example of opening a file for output (writing).

ofstream outputFile; outputFile.open("Employees.txt");

An _________ file is a file to which data is written. It is called an output file because the program stores output in it.

output

to use the stream insertion operator (<<) with the cout object to write data to the screen. It can also be used with ofstream objects to write data to a file. Assuming outputFile is an ofstream object, the following statement demonstrates using the << operator to write a string literal to a file:

outputFile << "I love C++ programming\n";

Here is a statement that writes both a string literal and the contents of a variable to a file:

outputFile << "Price: " << price << endl;

When you work with a ___________ (also known as a direct access file), you can jump directly to any piece of data in the file without reading the data that comes before it.

random-access file

Opening an input file allows the program to ______ data from the file.

read

When a file has been opened for input, the file stream object internally maintains a special value known as a ________

read position

There are two general ways to access data stored in a file: _____________

sequential access and direct access.

When you work with a _____________, you access data from the beginning of the file to the end of the file.

sequential-access file

there are two types of files: ____________

text and binary

A _________ contains data that has been encoded as text, using a scheme such as ASCII or Unicode. Even if the file contains numbers, those numbers are stored in the file as a series of characters.

text file

Keep in mind when the >> operator extracts data from a file, it expects to read pieces of data that are separated by ______ characters (spaces, tabs, or newlines).

whitespace

Opening an output file usually creates the file on the disk, and allows the program to ______ data to it.

write

Several flags may be used together if they are connected with the ____ operator.

|


Kaugnay na mga set ng pag-aaral

British Life and Visual Media Final

View Set

Chapter 21: Musculoskeletal System Test Bank—Nursing

View Set

The 6 components of skill related fitness

View Set

AP Psych Midterm: People to know

View Set

Chapter 1 Network Auth and Security

View Set

Emergency Care and Transportation of the Sick and Injured 12e CH30

View Set

Unit 7: NR328 - Nursing care: Altered cellular regulation in pediatrics

View Set